我需要读取,然后写入文件。我不想使用“r +”,因为我完全覆盖它。但我无法完全关闭该文件。如果我尝试第二次打开文件,则应用程序崩溃(无法打开)。有谁知道如何完全关闭文件。完全关闭c中的文件
这是不是实际的代码,只是一个是我想要做的总结:
FILE* f;
fopen_s(&f, "test.txt", "r");
// read file and edit data
fclose(f);
f = 0;
fopen_s(&f, "test.txt", "w");
fprintf(f, "%c", data);
fclose(f);
我不清楚,为什么你不能关闭文件?你正在尝试阅读,然后从你离开阅读的地方写下来? – Mike
什么是'fopen_s()'?使用标准函数['fopen()'](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html)我会测试返回值以检查操作是否成功......并且可能否则报告一个错误(函数['perror()'](http://pubs.opengroup.org/onlinepubs/9699919799/functions/perror.html))。 – pmg
我认为这是fopen的安全版本! – trumpetlicks