LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Prepend to ASCII text file in C (http://www.linuxquestions.org/questions/programming-9/prepend-to-ascii-text-file-in-c-814359/)

MS3FGX 06-15-2010 05:30 PM

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.

anomie 06-15-2010 05:39 PM

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).

theNbomr 06-15-2010 05:45 PM

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.

MS3FGX 06-15-2010 08:48 PM

Quote:

Originally Posted by anomie (Post 4004756)
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.

MS3FGX 06-22-2010 10:09 PM

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.


All times are GMT -5. The time now is 09:02 PM.