FILE I/O in reverse order?

M

Mark

Hi, I have an app that needs to maintain a log file of certain events that
occur. Normally FILE I/O appends new text to the end of a file. However
one of the requirements of my app is that the most recent log entries need
to be at the beginning of the file. My initial thought is that the file
would have to be rewritten every time a new log entry is added. Can anyone
suggest a better/simpler way?

An alternative would be to write the log as normal, then use a separate app
to post-process the log file into the most-recent-first order required.

Thanks alot!

Mark
 
K

Kevin Goodsell

Mark said:
Hi, I have an app that needs to maintain a log file of certain events that
occur. Normally FILE I/O appends new text to the end of a file. However
one of the requirements of my app is that the most recent log entries need
to be at the beginning of the file. My initial thought is that the file
would have to be rewritten every time a new log entry is added. Can anyone
suggest a better/simpler way?

There is no way, using standard C, to append data at the beginning of a
file. If such a method exists, it will be specific to your
implementation and you should ask about it on a group that discusses
your OS or compiler.

-Kevin
 
B

Ben Pfaff

Mark said:
Hi, I have an app that needs to maintain a log file of certain events that
occur. Normally FILE I/O appends new text to the end of a file. However
one of the requirements of my app is that the most recent log entries need
to be at the beginning of the file. My initial thought is that the file
would have to be rewritten every time a new log entry is added. Can anyone
suggest a better/simpler way?

An alternative would be to write the log as normal, then use a separate app
to post-process the log file into the most-recent-first order required.

I'd recommend this alternative. Not only does C not offer a way
to do what you want to do, I can't even think of a good way to do
it on common operating systems with OS-specific methods.
 
B

Billy O'Connor

Mark said:
Hi, I have an app that needs to maintain a log file of certain events that
occur. Normally FILE I/O appends new text to the end of a file. However
one of the requirements of my app is that the most recent log entries need
to be at the beginning of the file. My initial thought is that the file
would have to be rewritten every time a new log entry is added. Can anyone
suggest a better/simpler way?

An alternative would be to write the log as normal, then use a separate app
to post-process the log file into the most-recent-first order required.

I'd just read the file backwards.
 
D

Derk Gwen

# Hi, I have an app that needs to maintain a log file of certain events that
# occur. Normally FILE I/O appends new text to the end of a file. However
# one of the requirements of my app is that the most recent log entries need
# to be at the beginning of the file. My initial thought is that the file
# would have to be rewritten every time a new log entry is added. Can anyone
# suggest a better/simpler way?

Most operating systems you're likely to use today don't support anything
but a long string of bytes, and so neither will most C implementations. There
are many packages written in ANSI C that you can get, or write your own;
for example you can use something like Berkely DB and then write with
decreasing keys so that logical organisation is most recent first. However
such files are unlikely to be usable with most other programs.

# An alternative would be to write the log as normal, then use a separate app
# to post-process the log file into the most-recent-first order required.

On systems like unix, if you write the file as character lines beginning with
a timestamp, you can easily rearrange the file order with 'sort -r'. And unless
your files are huge (like hundreds of megabytes), the total wall clock and
cpu time used by a simple minded approach will actually be less than trying
to be clever
 
M

Mantorok Redgormor

Kevin Goodsell said:
There is no way, using standard C, to append data at the beginning of a
file. If such a method exists, it will be specific to your
implementation and you should ask about it on a group that discusses
your OS or compiler.

-Kevin

Why not simply output the new data to a tmp file
then read in the old file outputting it to the tmp
file. Then just rename the tmp file to the old file?

This can be done using standard C.
 
B

Ben Peddell

Derk said:
On systems like unix, if you write the file as character lines beginning with
a timestamp, you can easily rearrange the file order with 'sort -r'. And unless
your files are huge (like hundreds of megabytes), the total wall clock and
cpu time used by a simple minded approach will actually be less than trying
to be clever

Instead of 'sort -r', why not just use tac?

yourprog | tac >outfile
or
yourprog >tmpfile; tac <tmpfile >outfile
 
K

Kevin Goodsell

Mantorok said:
Why not simply output the new data to a tmp file
then read in the old file outputting it to the tmp
file. Then just rename the tmp file to the old file?

This can be done using standard C.

Well, yes, you can do that. I meant that there's no way to write output
at the beginning of a file, pushing the old contents toward the end of a
file (like the insert mode of a text editor).

-Kevin
 
A

alphatan['a:lfa:ta2n]

Mark said:
Hi, I have an app that needs to maintain a log file of certain events that
occur. Normally FILE I/O appends new text to the end of a file. However
one of the requirements of my app is that the most recent log entries need
to be at the beginning of the file. My initial thought is that the file
would have to be rewritten every time a new log entry is added. Can anyone
suggest a better/simpler way?

An alternative would be to write the log as normal, then use a separate app
to post-process the log file into the most-recent-first order required.

Thanks alot!

Mark
Is it a candidate that, read the whole file contents to an array, then
reopen a again, write the new contents. Then, append the original one?

I'm not sure whether this is effective. Because I haven't yet read any
example writing in this style. And, normally, *log* file is big and
growing fast. So, if the file were too big, or the operating frequency
were too high, something bad would be expected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top