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 ...

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?

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 !