LinuxQuestions.org
Visit Jeremy's Blog.
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 06-15-2010, 05:30 PM   #1
MS3FGX
Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
Prepend to ASCII text file in C


To start off I would like to acknowledge that I am not a very good C programmer and pretty much everything I know has been self taught through mostly trial and error. So forgive me if there is an obvious answer to my question, or if I don't immediately grasp the concepts involved in the possible solution.

Basically, I'm writing an application which will be creating log file entries rather rapidly (potentially hundreds per minute), and I would like each new line to appear at the top of the log file, rather than the end. Opening a text file in append mode is easy enough, but I can't seem to find any obvious way to do the opposite.

I have been looking online and it seems that there exists no standard way to do this, and I have only been able to find a few mentions of how somebody might achieve it. The most common method seems to be using two files and copying the data back and forth between them. This seems like it would be insanely I/O intensive with the number of lines I'm likely to be generating. If this is the best method to use, I will give it a shot; though I am not 100% clear on how to implement it, so I would appreciate any examples or guidance to that end.

I am also open to any other ideas as to how to accomplish this, and I don't have to worry about portability since the program already uses Linux-only libraries. So calling out to sed or something is not necessarily out of the question (though I imagine performance would also be an issue there).

Thanks for any help with this issue.
 
Old 06-15-2010, 05:39 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora, Lubuntu, FreeBSD
Posts: 3,930
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by MS3FGX
I would like each new line to appear at the top of the log file, rather than the end. Opening a text file in append mode is easy enough, but I can't seem to find any obvious way to do the opposite.
...
I am also open to any other ideas as to how to accomplish this...
Probably not quite the answer you're looking for, but you might consider doing some post-processing (i.e. after the log file is rotated and a new one is being written to) with tac(1).
 
Old 06-15-2010, 05:45 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Your problem is requires a great deal of inefficiency. Each time you write a new record, you will have to open a new file, write the new record to the new file, open & read the entire original file onto the new file, in append mode. Close both files, delete the old file, and rename the new file to the old filename.
--- rod.
 
Old 06-15-2010, 08:48 PM   #4
MS3FGX
Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Original Poster
Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
Quote:
Originally Posted by anomie View Post
Probably not quite the answer you're looking for, but you might consider doing some post-processing (i.e. after the log file is rotated and a new one is being written to) with tac(1).
This could potentially work, I hadn't considered this approach before. Ideally I would like to do it all in the main code, but this sounds like a relatively workable B-Plan.
 
Old 06-22-2010, 10:09 PM   #5
MS3FGX
Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Original Poster
Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
For anyone who might read this later on, I eventually just ended up going with anomie's suggestion of using "tac". It is not exactly what I had in mind to start with, but now that it is implemented I have to say it does seem to work pretty well.

You can check out the software that uses it on my site.
 
  


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
[SOLVED] Bash: Problem to prepend text to variable Beaverfriend Programming 4 02-13-2010 10:12 AM
want to prepend to a text file, not append urentity Programming 7 08-03-2009 12:45 AM
How to save text file with ASCII code?? Nejad General 7 01-04-2009 02:17 AM
Convert ASCII text to an audio file ed_homeLinux Linux - Software 1 07-22-2005 12:30 PM
Displaying a text file in ascii?? bauld Linux - General 4 11-21-2001 07:33 PM


All times are GMT -5. The time now is 02:51 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