c - 如何从 c 中的文本文件中删除所有文本?

标签 c file fopen

<分区>

FILE* 类型的给定参数 fd ,例如:
FILE* fd = fopen("a.txt","w")

如何删除a.txt中的所有文本?

注意:我不知道文件的名称是什么(我正在编写一个函数,该函数获取有人已经在 main 中打开的 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);

最佳答案

你可以用write标志打开它

fopen(filename, "w")

如果文件已经存在,该文件将被新的空文件覆盖。

关于c - 如何从 c 中的文本文件中删除所有文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50451446/

相关文章:

c - 文件处理程序在 C 中无法按预期工作

c - 如何从 C 语言的文件中读取行?

C编程: reading and writing binary files

c++ - main() 中的 return 语句与 exit()

c - linux 上的 unix 域套接字?

c - 使用c删除ntfs中的文件

c++ - 从文件中读取十六进制值 (C++)

c - 具有递归调用的线性搜索算法?

macos - OSX bash 递归查找按大小排序的文件,文件夹中除外

php - 如何检查 PHP 流资源是否可读或可写?