我在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;
}
Run Code Online (Sandbox Code Playgroud)
它给出了以下输出:
Count of written objects …Run Code Online (Sandbox Code Playgroud)