在我的代码中,我使用如下的snprintf,并能够看到以下行为
char text[30] = {0};
snprintf(text, sizeof(text), "%s", "hello");
printf("Interm... %s\n", text);
snprintf(text, "%20s", text);
printf("At the end ... %s\n", text);
Run Code Online (Sandbox Code Playgroud)
Interm... hello
At the end ...
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,snprintf的source和destinaton是否相同,它会清除缓冲区.我希望输出是20s格式说明符.我不能在第一步中执行此操作,因为我需要追加多个字符串并在最后一步执行格式说明符.
是复制到临时缓冲区并从那里到原始缓冲区唯一可能的解决方案?你能不能对此有所了解.