C ++ clear file contents after specific position

I would like to use either fstream, or preferably QFile, to delete the contents of the file after a certain position (this is not the beginning or end of the file). Therefore, I first go to this position with QFile::seek(long)or equivalent at a constant time , and then I would like to remove the rest of the content, also at a constant time. Which approach do you recommend?

+3
source share
3 answers

You can use QFile::resizeto resize the file to the desired size. Sure he uses truncatebehind the scenes (see Andrew's article).

+8
source

Search...

#include <unistd.h>
int ftruncate(int fildes, off_t length);
int truncate(const char *path, off_t length);

...

, .

.

+3

Consider the new position after seek(), which will be the end of the file. When you finish working with the file, write everything from the very beginning to this point.

0
source

All Articles