我试图覆盖C中的文件的内容,目前我有:如何覆盖C文件?
FILE* file = fopen("filename.txt", "r+");
fprintf(file, "%d", 1); // regardless of what's in the file, i want to clear it and put 1 in there
...
// legacy code somewhere else in the code base. can't change.
rewind(file);
fprintf(file, "%d", 2);
fflush(file);
然而,这将无法正常工作。其结果将是:
1,21
每个后续号码将被写入到1的开头例如:
1,第21,31,41,...
我想知道是否有一种方法来始终覆盖所以下面的产生有什么文件:
1,2,3,4,...
任何援助将不胜感激。
谢谢。
编辑:
我已经改变了代码:
FILE* file = fopen("filename.txt", "w+");
的问题仍然存在。
只要打开它为“W +” – BlackBear 2012-02-06 18:41:10