By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

FILE I/O in reverse order?

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Mark wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #2

P: n/a
"Mark" <ma*************@excite.com> writes:
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.
Nov 14 '05 #3

P: n/a
"Mark" <ma*************@excite.com> writes:
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.
--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I'll interact with on any given
day."
--Billy Chambless
Nov 14 '05 #4

P: n/a
"Mark" <ma*************@excite.com> wrote:
# 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

--
Derk Gwen http://derkgwen.250free.com/html/index.html
GERBILS
GERBILS
GERBILS
Nov 14 '05 #5

P: n/a
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<e8***************@newsread1.news.pas.earthli nk.net>...
Mark wrote:
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


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.

--
nethlek
Nov 14 '05 #6

P: n/a
Derk Gwen wrote:
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

Nov 14 '05 #7

P: n/a
Mantorok Redgormor wrote:
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<e8***************@newsread1.news.pas.earthli nk.net>...
Mark wrote:

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

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #8

P: n/a
Mark wrote:
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.

--
Learning is to improve, but not to prove.

Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.