笔者初识snprintf时,以为其跟printf有异曲同工之处,阅读了手册后,才发现不以为然;遂记录下来:
int snprintf ( char * s, size_t n, const char * format, ... );
Write formatted output to sized buffer
Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by s (taking n as the maximum buffer capacity to fill).
If the resulting string would be longer than n-1 characters, the remaining characters are discarded and not stored, but counted for the value returned by the function.
A terminating null character is automatically appended after the content written.
After the format parameter, the function expects at least as many additional arguments as needed for format.
@param
char *s
Pointer to a buffer where the resulting C-string is stored.
The buffer should have a size of at least n characters.
size_t n
Maximum number of bytes to be used in the buffer.
The generated string has a length of at most n-1, leaving space for the additional terminating null character.
size_t is an unsigned integral type.
const char *format
C string that contains a format string that follows the same specifications as format in printf (see printf for details).
... (additional arguments)
Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string (or a pointer to a storage location, for n).
There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.
举个栗子
snprintf(addr, sizeof(addr), "%s", DEFAULT_ADDR);
上述语句即,将string DEFAULT_ADDR的内容写入到addr中,最大长度为sizeof(addr);
与snprintf相似的还有sprintf,在知道了snprintf用法后,相信大家通过名字就应该能猜出sprintf与snprintf的不同
没错就是少了一个n;
int sprintf ( char * str, const char * format, ... );
Write formatted data to string
Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by str.
The size of the buffer should be large enough to contain the entire resulting string (see snprintf for a safer version).
A terminating null character is automatically appended after the content.
After the format parameter, the function expects at least as many additional arguments as needed for format.
@param
char *str
Pointer to a buffer where the resulting C-string is stored.
The buffer should be large enough to contain the resulting string.
const char *format
C string that contains a format string that follows the same specifications as format in printf (see printf for details).
... (additional arguments)
Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string (or a pointer to a storage location, for n).
There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.
举个栗子
/* sprintf example */
#include <stdio.h>
int main ()
{
char buffer [50];
int n, a=5, b=3;
n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);// n = ARR_LEN
printf ("[%s] is a string %d chars long\n",buffer,n);
return 0;
}
output:
[5 plus 3 is 8] is a string 13 chars long
本文详细介绍了snprintf和sprintf这两个C语言字符串格式化函数的区别与使用方法。snprintf用于将格式化的输出写入到大小固定的缓冲区中,并在超出长度时进行截断。sprintf则直接写入缓冲区,不会检查缓冲区大小,因此可能存在溢出风险。
9790

被折叠的 条评论
为什么被折叠?



