0

当我使用此代码时

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;
    }

textconst 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

为什么我会出现这种奇怪的行为?

4

2 回答 2

5

你写了两次:

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

选一个

于 2013-03-18T02:49:30.307 回答
0

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

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

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

于 2013-03-18T02:49:42.627 回答