0

How can I cut the unnecesary space left at the end of the file with fstream? I've got a binary file ~250mb big and after certain operations I'm left with ~100mb of data left in file (at the beginning). The file however is still 250mb. How can I free up the disk space?

It's a lot of identical structs written to the file sequentially using:

fstream::write((char *)data_struct,sizeof(data_struct));

Sometimes the structs at the end of the file are no longer needed.

Vinz
  • 3,030
  • 4
  • 31
  • 52
maniek765
  • 37
  • 6
  • There is no general solution for this... We dont know your file structure – Vinz Jun 11 '15 at 08:50
  • @Vinzenz It's just a lot of identical structs written to the file sequentially (using `fstream::write((char *)data_struct,sizeof(data_struct)`) And sometimes the ones at the end of the file are not longer needed. – maniek765 Jun 11 '15 at 09:05

1 Answers1

2

File streams don't support truncation except when being opened. You'll need to close it and then use an OS specific method to set the precise length. For Windows and Linux that is:

Windows: SetEndOfFile()

Linux: truncate()/ftruncate()

Andy Brown
  • 11,766
  • 2
  • 42
  • 61