FILE I/O in reverse order?

Discussion in 'C Programming' started by Mark, Jan 10, 2004.

  1. Mark

    Mark Guest

    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
     
    Mark, Jan 10, 2004
    #1
    1. Advertising

  2. 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.
     
    Kevin Goodsell, Jan 10, 2004
    #2
    1. Advertising

  3. Mark

    Ben Pfaff Guest

    "Mark" <> 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
     
    Ben Pfaff, Jan 10, 2004
    #3
  4. "Mark" <> 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.
     
    Billy O'Connor, Jan 10, 2004
    #4
  5. Mark

    Derk Gwen Guest

    "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?

    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
     
    Derk Gwen, Jan 10, 2004
    #5
  6. Kevin Goodsell <> wrote in message news:<e8JLb.1534$>...
    > 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
     
    Mantorok Redgormor, Jan 10, 2004
    #6
  7. Mark

    Ben Peddell Guest

    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
     
    Ben Peddell, Jan 10, 2004
    #7
  8. Mantorok Redgormor wrote:
    > Kevin Goodsell <> wrote in message news:<e8JLb.1534$>...
    >
    >>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.
     
    Kevin Goodsell, Jan 10, 2004
    #8
  9. 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.
     
    alphatan['a:lfa:ta2n], Jan 12, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shannon Yoder

    How do I list records in reverse order?

    Shannon Yoder, Aug 14, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    407
  2. Guest
    Replies:
    1
    Views:
    430
    William F. Robertson, Jr.
    Oct 28, 2003
  3. dogbite
    Replies:
    4
    Views:
    729
    osmium
    Oct 10, 2003
  4. sahukar praveen

    reading a file in reverse order (bootom-top)

    sahukar praveen, Nov 25, 2003, in forum: C Programming
    Replies:
    20
    Views:
    802
    CBFalconer
    Dec 7, 2003
  5. Mark

    Re: FILE I/O in reverse order?

    Mark, Jan 12, 2004, in forum: C Programming
    Replies:
    0
    Views:
    384
Loading...

Share This Page