LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 07-03-2005, 07:03 AM   #1
iclinux
Member
 
Registered: Dec 2004
Posts: 69

Rep: Reputation: 15
how to clear a file's content?


is there any api to use?

Regards.
 
Old 07-03-2005, 07:14 AM   #2
jcliburn
Member
 
Registered: Dec 2003
Location: Mississippi, USA
Distribution: Fedora
Posts: 435

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

Code:
$ /bin/echo "" > filename
 
Old 07-03-2005, 07:24 AM   #3
cs-cam
Senior Member
 
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,544
Blog Entries: 4

Rep: Reputation: 56
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
 
Old 07-03-2005, 07:31 AM   #4
iclinux
Member
 
Registered: Dec 2004
Posts: 69

Original Poster
Rep: Reputation: 15
thanks, both.
BTW, is there any c API?
 
Old 07-03-2005, 07:36 AM   #5
Michael Johnson
Member
 
Registered: Jul 2005
Location: Wagga Wagga, Australia
Posts: 262

Rep: Reputation: 30
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
 
Old 07-03-2005, 07:55 AM   #6
jcliburn
Member
 
Registered: Dec 2003
Location: Mississippi, USA
Distribution: Fedora
Posts: 435

Rep: Reputation: 33
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.
 
Old 07-03-2005, 05:57 PM   #7
Michael Johnson
Member
 
Registered: Jul 2005
Location: Wagga Wagga, Australia
Posts: 262

Rep: Reputation: 30
You got it.
 
Old 03-30-2009, 02:01 PM   #8
ursusca
Member
 
Registered: Sep 2008
Location: Toronto, ON, Canada
Distribution: Gentoo, RHEL (Fedora, CentOS, OEL), Ubuntu, FreeBSD, Solaris 10
Posts: 170

Rep: Reputation: 34
echo -n > file
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i replace the clear "clear screen" with the cls command thefedexguy Suse/Novell 2 12-02-2005 05:02 PM
Shell script: substitute a file's content according to a map? Chowroc Programming 11 11-15-2005 04:08 AM
Help! tar file's name is truncated auden Linux - Newbie 1 03-02-2005 09:09 PM
Apache serving content after the content is removed? jrbush82 Linux - Software 6 05-05-2004 04:39 AM
file's extension??? amanjsingh Linux - Newbie 2 04-17-2004 09:22 AM


All times are GMT -5. The time now is 07:56 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration