我遇到了一个奇怪的行为,fwrite()
在我关闭流后成功,fclose()
但文件没有因为fflush()
失败而被覆盖。
我的代码是:
int main(int argc, char* argv[])
{
FILE* file = fopen("file.txt", "w");
if(!file) perror("Cannot open the file.\n");
char text[] = "1234567";
fclose(file);
int count_of_written_objects = fwrite(text, sizeof(text),1, file);
printf("Count of written objects into the file: %d \n", count_of_written_objects);
if(count_of_written_objects != 1) perror("Not expected count of objects was written.\n");
int success = fflush(file);
printf("Variable success: %d \n", success);
if(success == EOF) perror("Flush did not succeed. \n");
return 0;
}
它提供以下输出:
Count of written objects into the file: 1
Variable success: -1
Flush did not succeed.
: Bad file descriptor
fwrite()
关闭流时如何成功?可以fwrite()
在关闭的流上写吗?你能给我解释一下吗?