Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > FILE I/O in reverse order?

Reply
Thread Tools

FILE I/O in reverse order?

 
 
Mark
Guest
Posts: n/a
 
      01-10-2004
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


 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      01-10-2004
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.
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-10-2004
"Mark" <(E-Mail Removed)> 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
 
Reply With Quote
 
Billy O'Connor
Guest
Posts: n/a
 
      01-10-2004
"Mark" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Derk Gwen
Guest
Posts: n/a
 
      01-10-2004
"Mark" <(E-Mail Removed)> 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
 
Reply With Quote
 
Mantorok Redgormor
Guest
Posts: n/a
 
      01-10-2004
Kevin Goodsell <(E-Mail Removed)> wrote in message news:<e8JLb.1534$(E-Mail Removed) 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
 
Reply With Quote
 
Ben Peddell
Guest
Posts: n/a
 
      01-10-2004
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

 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      01-10-2004
Mantorok Redgormor wrote:
> Kevin Goodsell <(E-Mail Removed)> wrote in message news:<e8JLb.1534$(E-Mail Removed) 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.
 
Reply With Quote
 
alphatan['a:lfa:ta2n]
Guest
Posts: n/a
 
      01-12-2004
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.

 
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
Reverse file order in folder? Kenny Computer Support 2 10-23-2005 05:38 PM
getResourceAsStream() in reverse to create a file Roger Varley Java 2 02-18-2005 01:45 PM
Re: Reverse function of Server.MapPath(string file)? Curt_C [MVP] ASP .Net 0 01-22-2004 05:36 PM
Reverse engineering an EDIF file? Rastislav Struharik VHDL 8 01-02-2004 01:57 PM
Stacks Queues Reverse Reverse Polish dogbite C++ 4 10-10-2003 05:06 AM



Advertisments