Mikhail
Mikhail

Reputation: 7

How to clear the contents of an open file in *nix

I would like to know how the contents of a file can be cleared in *nix if it is open to write. (It may be a log file for example.)

Upvotes: 0

Views: 145

Answers (1)

rath
rath

Reputation: 4059

Take a look at fopen's manpage:

w

Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

so if you use

fp=fopen("file.txt", "w");

the contents of file.txt will be erased.

Update:

To delete a file's contents from command line use

printf "\0" > file.txt

Upvotes: 1

Related Questions