LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 09-06-2010, 07:46 AM   #1
whirlpool
LQ Newbie
 
Registered: Aug 2010
Posts: 11

Rep: Reputation: 3
How to avoid overwriting a file


Hey,

I'm facing difficulty while writing to an already existing file . I'm making a shell (programming in C). I want a file (say , logfile) to keep a record of all the commands a user enters. For the first command it runs fine , but for the second time and thereafter it overwrites the previous contents. How do I avoid this?

Thanks
Ambika
 
Old 09-06-2010, 07:54 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I'm not a C programmer, but like any other language it has the possibility to append rather then overwrite a file.

Feed your search engine with: programming C append to get dozens of links.

Hope this helps.
 
Old 09-06-2010, 08:00 AM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Use fopen() with append mode !
http://www.opengroup.org/onlinepubs/...ons/fopen.html

What function are you using for opening/writing files ?

Last edited by Aquarius_Girl; 09-06-2010 at 08:01 AM.
 
Old 09-07-2010, 12:47 AM   #4
whirlpool
LQ Newbie
 
Registered: Aug 2010
Posts: 11

Original Poster
Rep: Reputation: 3
Thanks

Thanks for the help.
I used open and close commands. The problem was I was opening the file again and again using a for loop. Opening it only once and then appending it solved my problem.
 
  


Reply



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
apache file overwriting hopefull Linux - Server 6 01-26-2009 02:54 PM
FIle Overwriting ZAMO Linux - General 3 08-08-2007 10:46 AM
How do I redirect something to a file without it overwriting the destination file??? nikold01 Linux - General 8 09-09-2004 02:30 PM
Avoid overwriting bootloader jwijesundra Linux - General 5 07-30-2004 05:16 AM
How to avoid executing embeddbed file/link satimis Linux - Security 4 01-15-2004 08:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:51 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration