C function fwrite() doesn't write in file

做~自己de王妃 提交于 2019-12-24 17:20:04

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/29172591/c-function-fwrite-doesnt-write-in-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!