Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C function fwrite() doesn't write in file

Tags:

c

io

file-io

fwrite

I'm trying to write structures from tempGroupFile into GroupFile. fwrite() returns 1, when writing, but actually no data is written in the file GroupFile. Function printRec() prints out the structure on the screen. data is a variable of structure. File GroupFile is empty after these operations. Code:

GWTemp = fopen(tempGroupFile, "rb");
GW = fopen(GroupFile, "wb"); 
if((GW == NULL) || (GWTemp == NULL))
{
    puts("Failed to open file.");
    fflush(stdin);
    getchar();
    return 0;
}
while(fread(&data, sizeof data, 1, GWTemp))
{
    if(fwrite(&data, sizeof data, 1, GW))
    {
        printRec(data);
    }
}
like image 709
Nazarii Plebanskii Avatar asked Oct 26 '25 10:10

Nazarii Plebanskii


1 Answers

You need to close the file using fclose(GW) after the while loop. This makes sure all buffers are flushed so the file is written.

like image 58
blh83 Avatar answered Oct 28 '25 23:10

blh83



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!