Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I misuse fwrite function?

Tags:

c

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;
}
like image 403
Marco Avatar asked Sep 21 '26 16:09

Marco


2 Answers

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

like image 78
Kamyar Souri Avatar answered Sep 24 '26 07:09

Kamyar Souri


Add fclose(fp); before returning.

like image 43
Brett Hale Avatar answered Sep 24 '26 07:09

Brett Hale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!