Marco
Marco

Reputation: 1025

Do I misuse fwrite function?

I do not understand why the following code can create the aaa file, but cannot write '1' (the buffer value) into the aaa file.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a;
    int b;
    int buf;
    FILE *fp;

    buf = 1;
    a = 5;
    b = 6;
    fp = fopen("c:\\aaa.txt","wb");
    fwrite(&buf, sizeof(int),1,fp);

    return 0;
}

Upvotes: 2

Views: 172

Answers (2)

Kamyar Souri
Kamyar Souri

Reputation: 1923

call fclose(fp) or fflush(fp) to make sure content of file buffer gets flushed to the file.

Upvotes: 6

Brett Hale
Brett Hale

Reputation: 22308

Add fclose(fp); before returning.

Upvotes: 4

Related Questions