[英]sprintf writing a blank string when used with %*.s format specifier
I have an sprintf as follows - 我有一个sprintf如下-
sprintf (output,"%.*s%s%s%s",length,Str1,Str2,Str3,Str4);
All the strings contain valid data and the length parameter as well is correct. 所有字符串都包含有效数据,并且length参数也正确。 Yet, output remains emtpty after this sprintf. 但是,在此sprintf之后,输出仍然为空。
If I replace %.*s with %s and remove the length parameter, it works perfectly fine. 如果我将%。* s替换为%s并删除length参数,则效果很好。
Try This 尝试这个
Change 更改
sprintf (output,"%*.s%s%s%s",length,Str1,Str2,Str3,Str4);
To 至
sprintf (output,"%.*s%s%s%s",length,Str1,Str2,Str3,Str4);
^^
The reason why nothing is being output is because if you omit the trailing digit or star after the dot, the precision is taken to be zero. 之所以没有输出任何内容,是因为如果您在点后省略了尾随数字或星号,则精度将被视为零。 %*.
modifies the width not the precision . 修改宽度而不是精度 。 Instead, you want %.*
. 相反,您需要%.*
。 Note that this only applies to the conversion specifier it is a part of. 请注意,这仅适用于它所属的转换说明符。 ie: 即:
char output[100];
char str1[] = "hello";
char str2[] = "there";
int length = 4;
sprintf(output, "%.*s%s", length, str1, str2);
printf("%s", output);
Output: 输出:
hellthere
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.