开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜