【发布时间】:2018-10-31 06:19:42
【问题描述】:
给定参数fd,类型为FILE*,例如:FILE* fd = fopen("a.txt","w")。
如何删除a.txt中写的所有文字?
注意:我不知道文件的名称是什么(我正在编写一个函数,该函数以 FILE* 类型的参数获取某人已经在主目录中打开的参数)。
例如:
FILE* fd = fopen("a.txt","w");
assert(fd != NULL); // it's not important for this question.
fprintf(fd,"hello1\n");
fprintf(fd,"hello2\n");
//.... and now I want to remove all the text from a.txt. How can I do it?
// The cleaning will be in other function that get just fd (without the
// name of the file)
fclose(fd);
【问题讨论】:
-
freopen将完成这项工作 -
我可以用 print "" 以某种方式做到这一点(并且无需再次打开文件吗?)
-
@JohnnyMopp 我编辑了我的问题。
-
@Software_t:这会改变一切(使答案无效,但使问题变得有趣)。
-
@Bathsheba 在那之后,我看到了答案,我明白这很重要(我确信通过将空字符串打印到文件或类似的东西以某种方式存在)。