简体   繁体   中英

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.

snprintf ,它也有一个size参数:

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

snprintf ,但是如果您打印N个字符,则它不会为null终止。

大多数编译器都有一个snprintf()函数。

您需要snprintf()。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM