#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    athome
    Posts
    50
    Rep Power
    13

    Question fprintf - file input


    hi,

    i know i'm really a beginner in ANSI C. I have to fill in a textfile with logs. The problem is that the last input is the last line in the textfile.

    I want that the last input is the first line? How can I achieve this the easiest way? To read everything out and fill it in afterwards would be quite unefficient?

    cu
    EP
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Hi,

    As far as I know, you can only do two things when writing to a file: you can add to the end of the file, or you can overwrite something in the file.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    athome
    Posts
    50
    Rep Power
    13

    fprintf


    i know that i can only open files with serveral flags, which only allow appending info or deleting the file and inserting new info.

    but i need a short algorithm which allows inserting lines at the beginning and doesn't delete the other lines.

    thx in advance
    cu
    EP
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "To read everything out and fill it in afterwards would be quite unefficient?"

    My point was, as far as I know, you don't have any other options.

    It doesn't sound very hard. You could read the current lines of the file into strings, then write the current log to the file, and subsequently write the strings back to the file.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,158
    Rep Power
    2222
    Or another kludge would be:
    1. create a new file.
    2. write the new log entry to it. Keep the new file open.
    3. open the log file for read.
    4. read each line of the log file and write it to the new file.
    5. close both files.
    6. delete the log file
    7. rename the new file to the log file's name.

    Instead of deleting the log file, you could keep a series of backup copies just in case, similar to Linux's messages log files -- messages, messages.1, messages.2, messages.3, messages.4 etc.

    In that case, steps 6 & 7 would be:
    6. delete the oldest log-file back-up (eg, log.4)
    7. rename the next-oldest to the oldest's name
    (e.g., rename("log.3","log.4");
    8. similarly bump the names of the other log files:
    log.2 -> log.3
    log.1 -> log.2
    log -> log.1
    9. now rename the new file to the log-file name.

    Not very elegant, but it should work.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    athome
    Posts
    50
    Rep Power
    13

    thx


    thanks for your advice.
    was quite a help for me.

    cu
    w2k-user

IMN logo majestic logo threadwatch logo seochat tools logo