Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > To delete few lines and add few lines at the end of a text file using c program

Reply
Thread Tools

To delete few lines and add few lines at the end of a text file using c program

 
 
Murali
Guest
Posts: n/a
 
      03-09-2006
Hello Everyone,


I am breaking my head with this problem. Please help
me out. Let me first explain my problem :

Here it is:

I am working in realtime environment where i will be creating
logfiles internally while my system is running.
I am having a logfile.txt in which i have to store maximum of 150
lines.Suppose for every 60 seconds 50 lines are going to add to my
logfile If it exceeds more than 150 my program should remove first 50
lines and add coming 50 lines at the end.

In brief to say how to delete few lines at the top of a text file and
append it 50 lines at bottom.


It will be very helpful for me if you send me any system call to solve
my problem or the code.

Thank you,
Murali

 
Reply With Quote
 
 
 
 
Ben Pope
Guest
Posts: n/a
 
      03-09-2006
Murali wrote:
>
> In brief to say how to delete few lines at the top of a text file and
> append it 50 lines at bottom.


You'll have to rewrite the entire file.

The general form is:
Read in old file, modify, write temporary file
Delete old file
Rename temporary file to old file name.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      03-09-2006
In article <1141912411.265479.258040
@p10g2000cwp.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> Hello Everyone,
>
>
> I am breaking my head with this problem. Please help
> me out. Let me first explain my problem :
>
> Here it is:
>
> I am working in realtime environment where i will be creating
> logfiles internally while my system is running.
> I am having a logfile.txt in which i have to store maximum of 150
> lines.Suppose for every 60 seconds 50 lines are going to add to my
> logfile If it exceeds more than 150 my program should remove first 50
> lines and add coming 50 lines at the end.


As stated, this simply isn't possible (at least not
portably).

Your choices are 1) create something more or less along
the lines of a database, or 2) create the file anew each
time it's needed.

Given the size of file, the latter is probably the better
option in this case unless your lines are _really_ long.
With typical log file entries, you're looking at 150x40
(average) => around 6K. That just doesn't justify the
overhead of a database style of format (which wouldn't be
readable as normal text anyway).

If you have some memory available, I'd consider reading
the data in at startup, holding it in a circular buffer
in memory, and writing it out as needed. This way you're
not reading the old file back into memory on a regular
basis, cutting your I/O roughly in half.

If the log file is really crucial, instead of overwriting
the old log file, you're probably better off writing the
data to a new file, then deleting the old file and
renaming the new file to the old name.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
add a 3 lines of text in a file before "package" in file mike Perl Misc 3 09-09-2009 02:11 AM
Newbie: add few lines to file ..? newman Ruby 3 01-31-2008 02:54 PM
How do I tell the difference between the end of a text file, and an empty line in a text file? walterbyrd Python 7 05-17-2007 06:02 AM
Preserve blank lines when add multiple lines of text to a cell Cah Sableng Javascript 0 04-23-2007 04:46 AM
Reading just a few lines from a text file tkpmep@hotmail.com Python 6 08-23-2005 11:11 PM



Advertisments