简体   繁体   English

C函数fwrite()未写入文件

[英]C function fwrite() doesn't write in file

I'm trying to write structures from tempGroupFile into GroupFile . 我试图将结构从tempGroupFile写入GroupFile fwrite() returns 1, when writing, but actually no data is written in the file GroupFile . fwrite()在写入时返回1,但实际上没有数据写入文件GroupFile Function printRec() prints out the structure on the screen. 函数printRec()在屏幕上打印出结构。 data is a variable of structure. data是结构变量。 File GroupFile is empty after these operations. 这些操作后,文件GroupFile为空。 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);
    }
}

You need to close the file using fclose(GW) after the while loop. 在while循环之后,您需要使用fclose(GW)关闭文件。 This makes sure all buffers are flushed so the file is written. 这样可确保刷新所有缓冲区,以便写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM