LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to clear a file's content? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-clear-a-files-content-339566/)

iclinux 07-03-2005 07:03 AM

how to clear a file's content?
 
is there any api to use?

Regards.

jcliburn 07-03-2005 07:14 AM

I'm not quite sure what you have in mind, but one way to empty a file is:

Code:

$ /bin/echo "" > filename

cs-cam 07-03-2005 07:24 AM

Or if you want to jumble it up and make the contents unretreivable:
Code:

cat /dev/urandom > filename
Just hit Ctrl-C after a second or so or you'll find yourself with a pretty large file or crap. I just tried that and it's sort of fun :D

iclinux 07-03-2005 07:31 AM

thanks, both.
BTW, is there any c API?

Michael Johnson 07-03-2005 07:36 AM

Just open the file in write only mode and it will be truncated to a length of 0.
#man fopen
will give you the full details

jcliburn 07-03-2005 07:55 AM

Quote:

Originally posted by iclinux
thanks, both.
BTW, is there any c API?

Now I understand, I think. You want to do this in C. Examine the manpage for fopen. You'll need to open the file in write mode, not append mode. For example:

Code:

fopen ("path_to_file", "w");
Opening the file this way will zero its contents.

Michael Johnson 07-03-2005 05:57 PM

You got it.

ursusca 03-30-2009 02:01 PM

echo -n > file


All times are GMT -5. The time now is 09:24 PM.