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]、関数の引数の1つとして設定されます)

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

あなたは2回書いています:

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

一つを選ぶ

于 2013-03-18T02:49:30.307 に答える
0

これらの2行は、「テキスト」を2回書き込みます。彼らは同じことをします。

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

唯一の違いは、fprintfがfwriteよりも1バイト多い'\0'を書き込むことです。

于 2013-03-18T02:49:42.627 に答える