LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-04-2007, 11:05 PM   #1
tinieprotonjam
LQ Newbie
 
Registered: Dec 2006
Posts: 28

Rep: Reputation: 15
Question how can i clear the contents of a file from a C program?


I want to erase the contents of a text file from my program. I have read that in the terminal window i can type:

Code:
 :>filename
How will I be able to achieve something like this in my C program?

Thanks a lot.
 
Old 01-04-2007, 11:47 PM   #2
kotnik
Member
 
Registered: Nov 2004
Location: Novi Sad, Serbia
Distribution: Debian, Slackware, Gentoo, openSuSE
Posts: 254

Rep: Reputation: 31
http://www.opengroup.org/pubs/online...xsh/fopen.html
 
Old 01-05-2007, 12:13 AM   #3
tinieprotonjam
LQ Newbie
 
Registered: Dec 2006
Posts: 28

Original Poster
Rep: Reputation: 15
Question

Quote:
Originally Posted by kotnik
but there's nothing here that I can see that can help me clear the contents of the file, unless I manually replace the contents of the file with white spaces.... is there any other way, like passing :>filename to a certain function?

thanks.
 
Old 01-05-2007, 12:29 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

Rep: Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022
The point he is making is that opening for write will truncate the file; see mode 'w'
 
Old 01-05-2007, 01:24 AM   #5
tinieprotonjam
LQ Newbie
 
Registered: Dec 2006
Posts: 28

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01
The point he is making is that opening for write will truncate the file; see mode 'w'
thanks for the explanation, I was able to make it work. Thanks a lot.
 
Old 01-05-2007, 11:37 AM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
truncate("filename", (off_t) 0);

truncates an existing file as well.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
failed to send 'clear' to local ypserv: RPC:Program not registered beginner16 Linux - Newbie 3 04-29-2014 12:25 PM
RIP 1 question : How to access the contents of a routing table within a program ? prashanth s j Linux - Networking 1 10-25-2005 08:27 AM
How to access the contents of routing table within a program?(for RIP1) prashanth s j Linux - Networking 1 09-03-2005 04:29 AM
clear file? Red Squirrel Linux - Software 5 07-19-2005 09:27 PM
Eays way to clear a log file? extremebfn Linux - General 2 01-16-2005 04:05 AM


All times are GMT -5. The time now is 07:14 AM.

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