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:
我正在尝试将tempGroupFile中的结构写入GroupFile。写入时fwrite()返回1,但实际上没有数据写入文件GroupFile。函数printRec()打印出屏幕上的结构。数据是结构的变量。这些操作后,文件GroupFile为空。码:
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 个解决方案
#1
2
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.
您需要在while循环后使用fclose(GW)关闭文件。这样可以确保刷新所有缓冲区以便写入文件。
#1
2
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.
您需要在while循环后使用fclose(GW)关闭文件。这样可以确保刷新所有缓冲区以便写入文件。