简体   繁体   English

如何删除不带文件名的文件内容?

[英]How do you get rid of file contents without the filename?

I have a function that takes in a file pointer ( FILE * file ) and copies everything in that file. 我有一个函数,它接受文件指针( FILE * file )并复制该文件中的所有内容。 Now, is it possible to erase everything inside this file? 现在,是否可以擦除此文件中的所有内容?

I do not have the file name so I can not use fopen(filename, "w") again. 我没有文件名,因此无法再次使用fopen(filename, "w")

I have stdio.h and string.h included. 我有stdio.hstring.h

You can use the ftruncate() system call to empty a file using its file descriptor, eg 您可以使用ftruncate()系统调用使用文件描述符来清空文件,例如

ftruncate(fileno(fh), 0);

You will probably want to follow that up with a call to rewind(fh) so that any further writes to the file are made at the beginning, rather than at the previous offset. 您可能希望通过调用rewind(fh)来进行后续操作,以便对文件的任何进一步写入都在开始处进行,而不是在先前的偏移处进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM