[英]C language printf add additional useless message
I tried to simulate the linux file permission use st_mode, but when I print the result, it has an additional unwanted message.我试图模拟 linux 文件权限使用 st_mode,但是当我打印结果时,它有一个额外的不需要的消息。
/* file permission */
char buf[9] = {0};
char tmp_buf[] = "rwxrwxrwx";
int i;
for(i = 0; i < 9; i++) {
if (sta.st_mode & (1 << (8 - i))) buf[i] = tmp_buf[i];
else buf[i] = '-';
}
printf("%s", buf);
Here is my code, but the result is something like 'rw-rw-r--rwxrwxrwx' and the strlen(buf) is 18.... Can someone help?这是我的代码,但结果类似于 'rw-rw-r--rwxrwxrwx' 并且 strlen(buf) 是 18.... 有人可以帮忙吗? I am not sure why temp_buf is appended to 'buf'
我不确定为什么 temp_buf 被附加到 'buf'
The conversion specifier %s
expects an argument that represents a string.转换说明符
%s
需要一个表示字符串的参数。 However it seems that the array buf
does not contain a string.但是,数组
buf
似乎不包含字符串。
Either enlarge the array with one more byte for the terminating zero要么用一个字节扩大数组以作为终止零
char buf[10] = {0};
or just use (specifying the precision before the conversion symbol s
)或仅使用(在转换符号
s
之前指定精度)
printf("%.9s", buf);
or alternatively或者
printf( "%.*s", ( int )sizeof( buf ), buf );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.