如果没有正确使用fclose函数,会怎样?
一个很老的帖子
关于文件打开后写入数据未关闭而再次打开再次写入数据的问题
想问一下,上面那个帖子中二楼给出的几个测试程序,是如何工作的?
代码1
my_file = fopen("11.txt", "w");
fprintf(my_file,"start!\n");
//fclose(my_file);//在此处不关闭文件
my_file = fopen("11.txt", "a");
fprintf(my_file,"my file\n");
fclose(my_file);
代码2
my_file = fopen("11.txt", "w");
fprintf(my_file,"start!\n");
fclose(my_file);//在此处不关闭文件
my_file = fopen("11.txt", "a");
fprintf(my_file,"my file\n");
//fclose(my_file);
代码3
my_file = fopen("11.txt", "w");
fprintf(my_file,"start!\n");
//fclose(my_file);//在此处不关闭文件
my_file = fopen("11.txt", "a");
fprintf(my_file,"my file\n");
//fclose(my_file);
代码4
my_file = fopen("11.txt", "a");//此处也已追加方式
fprintf(my_file,"start!\n");
//fclose(my_file);//在此处不关闭文件
my_file = fopen("11.txt", "a");
fprintf(my_file,"my file\n");
fclose(my_file);
打开文件和关闭文件都是比较耗时的。
如果可以不关闭也没有多大关系的!
只要你在下次对文件进行操作的时候注意文件流句柄的位置就好,
防止覆写或者读到空白数据就好!
[解决办法]
用fflush代替。