Thread: Delete/Overwire Lines from a text file

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Red face Delete/Overwire Lines from a text file

    Hi,

    I want to overwrite or delete the last 'x' lines of a text file.

    I don't mind if they are deleted and then I write new data or if they are just overwritten as the new data in entered.

    Alternatively, I could open a new file. Copy the contents of the file with the info over to this new file, minus the last 20 lines. Then I can delete the original file and then rename the new file as the same name of the file I just deleted.

    Can anyone help me with this? Which do you think would be easiest to implement?

    I think the 3rd method of creating a new file could be easiest but is there a function which I can use which will allow me to copy over the lines of the file - the last 'x' lines?

    Please help,

    cheers

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I think (Read -> Write to new file -> Rename ) is 'cleaner' approach.
    You can use C's rename for the last step.

    >I think the 3rd method of creating a new file could be easiest but is there a function which I can use which will allow me to copy over the lines of the file - the last 'x' lines?
    Read all of the original file, counting the number of lines.
    Write n - 20 of those lines to the new file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading lines of a text file
    By george7378 in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2011, 08:39 AM
  2. Number of lines in a text file
    By nbo10 in forum C Programming
    Replies: 3
    Last Post: 08-13-2007, 09:34 AM
  3. Best way to read lines out of a text file
    By movl0x1 in forum C Programming
    Replies: 9
    Last Post: 05-29-2007, 12:45 PM
  4. how do I count the lines in a text file?
    By slow brain in forum C Programming
    Replies: 4
    Last Post: 03-10-2003, 02:56 PM
  5. lines of a text file
    By face_master in forum C++ Programming
    Replies: 8
    Last Post: 11-06-2001, 07:09 AM