我在sprintf面临严重问题。
假设我的代码段是:
sprintf(Buffer,"Hello World");
sprintf(Buffer,"Good Morning");
sprintf(Buffer,"Good Afternoon");
.
.
.
几百个短跑......
如果我喜欢这样,它会被覆盖。
如何避免使用sprintf覆盖。如果我在最后给出printf,我想看到所有的行。
答案 0 :(得分:96)
你需要:
sprintf(Buffer,"Hello World");
sprintf(Buffer + strlen(Buffer),"Good Morning");
sprintf(Buffer + strlen(Buffer),"Good Afternoon");
当然你需要你的缓冲区足够大。
答案 1 :(得分:63)
int length = 0;
length += sprintf(Buffer+length, "Hello World");
length += sprintf(Buffer+length, "Good Morning");
length += sprintf(Buffer+length, "Good Afternoon");
这是一个对错误有抵抗力的版本。如果您在发生错误时不在乎它是有用的,只要您可以继续沿着您的快乐方式行事。
int bytes_added( int result_of_sprintf )
{
return (result_of_sprintf > 0) ? result_of_sprintf : 0;
}
int length = 0;
length += bytes_added(sprintf(Buffer+length, "Hello World"));
length += bytes_added(sprintf(Buffer+length, "Good Morning"));
length += bytes_added(sprintf(Buffer+length, "Good Afternoon"));
答案 2 :(得分:28)
为安全起见(缓冲区溢出)我建议使用snprintf()
const int MAX_BUF = 1000; char* Buffer = malloc(MAX_BUF); int length = 0; length += snprintf(Buffer+length, MAX_BUF-length, "Hello World"); length += snprintf(Buffer+length, MAX_BUF-length, "Good Morning"); length += snprintf(Buffer+length, MAX_BUF-length, "Good Afternoon");
答案 3 :(得分:12)
snprintfcat()
的{{1}}包装:
snprintf()
答案 4 :(得分:7)
使用sprintf()
Buffer += sprintf(Buffer,"Hello World");
Buffer += sprintf(Buffer,"Good Morning");
Buffer += sprintf(Buffer,"Good Afternoon");
答案 5 :(得分:6)
答案 6 :(得分:3)
您只是简单地附加字符串文字吗?或者您要添加各种数据类型(整数,浮点数等)?
可能更容易将其抽象为自己的函数(以下假定为C99):
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
int appendToStr(char *target, size_t targetSize, const char * restrict format, ...)
{
va_list args;
char temp[targetSize];
int result;
va_start(args, format);
result = vsnprintf(temp, targetSize, format, args);
if (result != EOF)
{
if (strlen(temp) + strlen(target) > targetSize)
{
fprintf(stderr, "appendToStr: target buffer not large enough to hold additional string");
return 0;
}
strcat(target, temp);
}
va_end(args);
return result;
}
你会像这样使用它:
char target[100] = {0};
...
appendToStr(target, sizeof target, "%s %d %f\n", "This is a test", 42, 3.14159);
appendToStr(target, sizeof target, "blah blah blah");
等
该函数返回vsprintf
的值,在大多数实现中,该值是写入目标的字节数。这个实现中有一些漏洞,但它应该给你一些想法。
答案 7 :(得分:3)
我认为您正在寻找fmemopen(3)
:
#include <assert.h>
#include <stdio.h>
int main(void)
{
char buf[128] = { 0 };
FILE *fp = fmemopen(buf, sizeof(buf), "w");
assert(fp);
fprintf(fp, "Hello World!\n");
fprintf(fp, "%s also work, of course.\n", "Format specifiers");
fclose(fp);
puts(buf);
return 0;
}
如果动态存储更适合您的用例,您可以遵循Liam关于使用open_memstream(3)
的优秀建议:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *buf;
size_t size;
FILE *fp = open_memstream(&buf, &size);
assert(fp);
fprintf(fp, "Hello World!\n");
fprintf(fp, "%s also work, of course.\n", "Format specifiers");
fclose(fp);
puts(buf);
free(buf);
return 0;
}
答案 8 :(得分:2)
我发现以下方法效果很好。
sprintf(Buffer,"Hello World");
sprintf(&Buffer[strlen[Buffer]],"Good Morning");
sprintf(&Buffer[strlen[Buffer]],"Good Afternoon");
答案 9 :(得分:1)
您可以使用下面显示的简单行将字符串附加到一个缓冲区中:
sprintf(Buffer,"%s %s %s","Hello World","Good Morning","Good Afternoon");
答案 10 :(得分:1)
使用strcat http://www.cplusplus.com/reference/cstring/strcat/
int main ()
{
char str[80];
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");
puts (str);
return 0;
}
Output:
these strings are concatenated.
答案 11 :(得分:0)
我编写了一个函数支持动态变量字符串append,就像PHP str append:str。 str。 ......等等。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
int str_append(char **json, const char *format, ...)
{
char *str = NULL;
char *old_json = NULL, *new_json = NULL;
va_list arg_ptr;
va_start(arg_ptr, format);
vasprintf(&str, format, arg_ptr);
// save old json
asprintf(&old_json, "%s", (*json == NULL ? "" : *json));
// calloc new json memory
new_json = (char *)calloc(strlen(old_json) + strlen(str) + 1, sizeof(char));
strcat(new_json, old_json);
strcat(new_json, str);
if (*json) free(*json);
*json = new_json;
free(old_json);
free(str);
return 0;
}
int main(int argc, char *argv[])
{
char *json = NULL;
/*
str_append(&json, "name: %d, %d, %d", 1, 2, 3);
str_append(&json, "sex: %s", "male");
str_append(&json, "end");
str_append(&json, "");
str_append(&json, "{\"ret\":true}");
*/
int i;
for (i = 0; i < 100; i++) {
str_append(&json, "id-%d", i);
}
printf("%s\n", json);
if (json) free(json);
return 0;
}
答案 12 :(得分:0)
完整的小代码示例
仅使用平面 stdio 标准库
#include <stdio.h>
int main()
{
char c[1024];
int i=0;
i+=sprintf(c+i,"We " );
i+=sprintf(c+i,"Love " );
sprintf(c+i,"Coding");
printf("%s",c);
}
输出:我们喜欢编码
答案 13 :(得分:0)
char string1[] = "test";
char string2[] = "string";
int len = sizeof(string1) + sizeof(string2);
char totalString[len];
sprintf(totalString, "%s%s",string1,string2);
答案 14 :(得分:-2)
使用strcat(buffer
,&#34; Your new string...here
&#34;)作为选项。
答案 15 :(得分:-4)
怎么样:
char s[100] = "";
sprintf(s, "%s%s", s, "s1");
sprintf(s, "%s%s", s, "s2");
sprintf(s, "%s%s", s, "s3");
printf("%s", s);
但考虑到可能的缓冲区过流!