我有一个char * temp_string
我在这里保留这些字符:Hello\nWor'ld\\\042
包括\0
最后。格式化与未格式化的字符串C
这是我做我的字符串的方式:
char * addDataChunk(char * data,char c)
{
char * p;
if(data==NULL)
{
if(!(data=(char*)malloc(sizeof(char)*2))){
printf("malloc error\n");
throwInternError();
}
data[0]=c;
data[1]='\0';
return data;
}
else
{
if((p = (char*)realloc(data,((strlen(data)+2)*sizeof(char))))){
data = p;
}
else{
printf("realloc error\n");
throwInternError();
}
data[strlen(data)+1] = '\0';
data[strlen(data)] = c;
return data;
}
}
这是我如何使用addDataChunk:
temp_char =getc(pFile);
temp_string=addDataChunk(temp_string,temp_char);
当我做这些两行:
printf("%s\n","Hello\nWor'ld\\\042");
printf("%s\n",temp_string);
我得到这个:
Hello
Wor'ld\"
Hello\nWor'ld\\\042
有人知道为什么输出不同吗?
http://stackoverflow.com/help/mcve – ceving
显示[MCVE],否则问题会很快downvoted /关闭。 –
我编辑了这个问题。 – nocturne