开发者

Function equivalent to sprintf() with maximum number of characters to be copied?

开发者 https://www.devze.com 2022-12-27 19:49 出处:网络
function(char* name) { char sql[50]; sprintf(sql, \"select %s;\", name); } What\'s the best way to make sure only 50 chars of name are copied to sql in the case name is larger than what sql can hold
function(char* name)
{       
   char sql[50];
   sprintf(sql, "select %s;", name);
}

What's the best way to make sure only 50 chars of name are copied to sql in the case name is larger than what sql can hold? (sprintf with a N 开发者_开发技巧parameter?)

Thank You.


There is snprintf, which also takes a size parameter:

int snprintf(char *str, size_t size, const char *format, ...);


snprintf, although it does not null terminate if you print N characters.


Most compilers have an snprintf() function.


You want snprintf().

int snprintf(char *str, size_t size, const char *format, ...);
0

精彩评论

暂无评论...
验证码 换一张
取 消