Suppose I have a really large text file, say 100 million lines or 1 GB and I want to delete the last line. Is there anyway to do this without having to rewrite 99,999,999 lines to a new file and delete the old one? Suppose the file is really really large that the rewrite option is prohibitively expensive. What would you do to delete the last line then? Thank you.
how to delete the last line in a text file with 100M lines without having to rewrite the whole file?
2 Answers
You can open the file, read from the end backwards until you find the first line delimiter (normally LF or CR/LF, depending on platform), calculate the file offset at that point, and truncate the file to that file offset.
You should use a truncation function, but neither FILE*
nor iostream
support it.
However, there are usually OS-specific functions at the lower level to truncate a file.
If Unix, you may use ftruncate
, but you'll need to find the offset where you want to truncate first (does each line have a fixed size?).
Be careful that, if you have opened a FILE*
for finding the offset, you need to be sure to synchronize it with the lower level. You may simply fclose
the file, then reopen it with open for the ftruncate
of the file at the decided offset.
Similar questions: https://stackoverflow.com/a/873653/2741329 and https://stackoverflow.com/a/15154682/2741329