1

For instance if I called WriteFile to the end of a file, and later I wanted to delete the written bytes, how could I do this? Do I have to read the file's contents into a buffer, re-create the file, and write the desired bytes, or is there an easier way?

kaykun
  • 2,247
  • 2
  • 24
  • 37

2 Answers2

3

Seek to the file position you want to truncate from (if you're not already there), then call the aptly-named SetEndOfFile() function.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
1

On Posix complient systems (Unix, Linux and others (Windows 7 has the Posix layer again))

int truncate(const char *path, off_t length); 
int ftruncate(int fildes, off_t length);

http://www.opengroup.org/onlinepubs/009695399/functions/truncate.html

http://www.opengroup.org/onlinepubs/009695399/functions/ftruncate.html

Patrick Schlüter
  • 11,394
  • 1
  • 43
  • 48