Delete/Overwire Lines from a text file

This is a discussion on Delete/Overwire Lines from a text file within the C++ Programming forums, part of the General Programming Boards category; Hi, I want to overwrite or delete the last 'x' lines of a text file. I don't mind if they ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    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.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 09:39 AM
  2. Number of lines in a text file
    By nbo10 in forum C Programming
    Replies: 3
    Last Post: 08-13-2007, 10: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, 01: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

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