Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C fwrite()写入的字符数增加了一倍_C_Char_Fwrite - Fatal编程技术网

C fwrite()写入的字符数增加了一倍

C fwrite()写入的字符数增加了一倍,c,char,fwrite,C,Char,Fwrite,当我使用这个代码时 FILE *f = fopen(file, "wb"); fflush(f); if (f==NULL) { //perror(f); return 0; } else{ fwrite(text, sizeof(char), strlen(text), f); int i = fprintf(f, "%s", text); if (i>0) { fclose(f); return 1;

当我使用这个代码时

FILE *f = fopen(file, "wb");
fflush(f);
if (f==NULL) {
    //perror(f);
    return 0;
}
else{
    fwrite(text, sizeof(char), strlen(text), f);
    int i = fprintf(f, "%s", text);
    if (i>0) {
        fclose(f);

        return  1;
    }
text
const char text[1024000]
,设置为函数中的参数之一) 如果我写

This is a test
This is a test
为了测试它是否可以写入多行,它写入

This is a test
This is a testThis is a test
This is a test
为什么我会有这种奇怪的行为?

你写了两次:

fwrite(text, sizeof(char), strlen(text), f);
int i = fprintf(f, "%s", text);
选择一个你要写两遍的:

fwrite(text, sizeof(char), strlen(text), f);
int i = fprintf(f, "%s", text);
选择一个

这两行写两次“文本”。他们做同样的事情

fwrite(text, sizeof(char), strlen(text), f);
int i = fprintf(f, "%s", text);
唯一的区别是fprintf比fwrite多写一个字节“\0”。

这两行写“文本”两次。他们做同样的事情

fwrite(text, sizeof(char), strlen(text), f);
int i = fprintf(f, "%s", text);

唯一的区别是fprintf比fwrite多写一个字节“\0”。

Ha,比您快12秒;)哈,比你领先12秒;)