Like Tree1Likes

append data to file at file's start

This is a discussion on append data to file at file's start within the Windows Programming forums, part of the Platform Specific Boards category; Hello, For some reason i need to append new data (text line,every few minutes) to log file at start. What ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    append data to file at file's start

    Hello,
    For some reason i need to append new data
    (text line,every few minutes) to log file at start. What is proper way to do this . I'm using Dev-C , winapi.
    Thanks and regards,
    stefan

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Write out your new data to a new file
    Append the old file onto the end of the new file
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    hello,Salem,
    1)'Movefile' old log file to temporary name
    2)'createfile' new logfile , write new data
    3)''createfle' for reading from renamed old file
    4)'readfile' oldfile, 'writefile' into new file loop.

    I see this way, but in case of very long file - this can be wrong way, i hesitate.
    regards

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    What is the point of putting new data at the start of a file every few minutes?

    Most people append to the END of a file, because it is a really simple thing to do.

    How often do you READ the log file - once per day perhaps?
    If writing is frequent (a few minutes) and reading is rare (once a day), then consider appending the data to the end of the log file, then figure out some kind of "reverse file" operation you perform just once a day.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    Salem,
    Logfile is viewed by user time to time through Excel(file is source for imported data into Excel).
    To simplify things ,related to Excel, (i think) i need such 'at top' append.
    It is possible to construct Excel sheet to use database (constructed on logfile) ,so last record will appear in top of screen. But this needs more complex work on Excel, and probably will need some tweaking/adjusting on end user computer. Which i want to skip.
    regards

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,620
    Clearly you don't know excel all that well, and are letting your fear of it get in the way of doing things sensibly.

    If you overcome your fear and apply a little effort to learning excel, you would probably find it simpler to program excel to read the log file and display in reverse order (or display only the last few lines of the log file) rather than write your program to write new content at the beginning of a file.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    Yes,grumpy,
    i do not know Excel , i do not use it often than one time per 2 years.
    My colleague consulted me about this case.
    Anyway - logfile can be viewed with text viewer and on-top adding can be useful .
    regards

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Well if the first "field" of your log record is something like a date/time (that excel will recognise), then simply append the data to the file.

    When you load it into excel, all you need do is then sort in reverse order, and you're done.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    A bunch of cars are waiting at a stop light. When a new car arrives, every single car goes into reverse and backs up to make room for the arriving car at the front of the line...

    Sounds crazy, because it is.
    gardhr likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    Problem solved trough Excel->dataimport ->query... User opens 'book1.xls' , which imports logfile (.csv type - comma separated text). Sorting is stored and doesnt need user intervention. While constructing 'book1.xls' Excel asked for 'Microsoft query' to be installed. But final file contains all info needed, so ExcelViewer works allmost fine, without 'MS Query' installed. It seems .xls and .csv files can be delivered to end user as email.

    Brewbuck, can You expand Your idea a little bit?

    regards

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,399
    Brewbuck, can You expand Your idea a little bit?
    Seriously?

    He is saying that constantly rewriting a file to prepend data is foolish and prohibitively expensive when the alternatives are so efficient and readily available.

    Soma

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    thanks,phantomotap - i missed irony.

    on other hand - i dont agree with You - alternatives are not "so efficient and readily available". But it is question of taste.
    regards

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,620
    Quote Originally Posted by stefan63 View Post
    on other hand - i dont agree with You - alternatives are not "so efficient and readily available". But it is question of taste.
    The only measure I can think of by which prepending input to the start of a file is efficient is that it avoids programming in excel to display the bottom of the file at the top of the screen.

    In terms of where it is not so efficient, there are many possible measures.

    The file writing the program is forced to rewrite the entire file whenever it writes a line, since no real-world operating system or disk file system supports prepending. Every time a line is written to the file, the existing contents of the file must be read and rewritten (or the file copied, which functionally achieves the same thing). So, if you are writing a line of 80 characters to a 1 KB file (which isn't all that big), prepending requires 1KB of data to be read from the file, the new line to be written, and the 1KB to be rewritten to the file. That means the program doing the prepending will write data to disk that is many, many, times the amount of data it is attempting to save. With the 80 char/1KB example, to write 80 characters the program must actually read 1024 characters from disk and write 1104 characters back. Compared with appending which will read 0 characters from disk and write 80 characters. That is a lot of additional I/O.

    And what is one of the primary bottlenecks of real-world program performance? Disk I/O. Not because disk I/O is bad, but because it is often orders of magnitude slower than accesses of memory or execution of general (non-I/O) operations by the CPU.

    Excel is pretty efficient at reading files (from top to bottom, anyway). Once excel has read the file, it is not typically necessary to read or write it again in order to show the last line read (other than a few exceptions such as swapping the program to disk on machines with very small amounts of physical memory).

    So effectively you are proposing a scheme to slow down the program generating the data to a small fraction of the performance achievable by simply appending. For the negligible benefit of not having to tweak an excel spreadsheet/macro (once, ever, once you have learned how) and an almost insignificant runtime performance gain when running Excel.

    Just because it may be to your taste does not mean your approach is particularly useful or valuable.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,399
    But it is question of taste.
    No. It really really really really really really really is not.

    Wasting the clients bloody time because you don't understand the real world, which is almost exactly as grumpy described it*, is not a question of taste.

    Soma

    * It can actually be worse.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,620
    Quote Originally Posted by phantomotap View Post
    * It can actually be worse.
    I agree with you that my previous post was loaded with dewy eyed optimism. After all, it is all (drum roll) just software
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How i an append a file and write at its start
    By fredsilvester93 in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2012, 11:22 PM
  2. Append a temp file to a log file
    By Randellk02 in forum C Programming
    Replies: 9
    Last Post: 11-07-2010, 01:46 PM
  3. Append one file to another.
    By guillermoh in forum C Programming
    Replies: 6
    Last Post: 02-04-2008, 12:04 PM
  4. file append
    By rahulsk1947 in forum C Programming
    Replies: 2
    Last Post: 10-30-2007, 02:01 AM
  5. append file?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2001, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21