Thread: Deleting the last set of data written in text file

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    58

    Deleting the last set of data written in text file

    Hi everyone,

    I have a problem here, I had tired to log a few set of data inside a text file before running a couple of function. Is there anyway to delete the data entered at the end of the program if there was no error in my function?

    thanks alot!!

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    One way:
    Store all the information you want to write to the log in memory - a string array will work.
    When the program comes to an end, then decide what pieces of the information you want to write to the log.

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I think he wants to delete the file he reads from after the program terminates. Is that right ?

    If it's the case, look at the remove function.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    I understand that I could have write it in the memory, but what if the program crushes before it can log all the information? Is there any other way to remove the data instead of read and rewrite all the data? All I need is to remove the last data that was log.

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You could read your data from one file, then write to another file (say log2). If your program crashes, log2 will contain up to where your program got before crashing.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    You could record the tell position within the file before you start writing your information to it. If your program completes successfully, you seek to that position and erase the remainder of the file. This of course assumes that you have other information in the file before your program writes to it and you wish to preserve it and also that you don't want to work with a copy of the original file.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    Thanks Happy reaper

    Let say if I want to keep it in one file? Is there a way to do it? Is there a way to go back to the last entry and overwrite the last entry if there is no error occur?

  8. #8
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    ftell() and fseek() or lseek() and ltell() depending on how you open the file in the first place.

  9. #9
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Well, if you really wanted to keep the name "log" for your file, just before your program concludes, you delete "log", and after that you could rename "log2" to "log", but if to keep that name during runtime, well, I don't know of a way to do that.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    Hei,

    Thanks I guess I get it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Deleting / Changing a record in a text file
    By clearrtc in forum C Programming
    Replies: 9
    Last Post: 08-21-2006, 12:09 AM
  5. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM