Search code examples
c++cfile-iofstreamfile-handling

Can we reduce size of existing file?


I want to know whether size of text or binary file can be reduced without creating temporary file. So that we can delete the data present at very end of file.

If yes then how. If no then why.


Solution

  • Yes, you can reduce file size using truncate() or ftruncate() functions that are available on POSIX systems.

    For Windows, use SetFilePointer to set the current position to desired file size, then call SetEndOfFile to actually truncate it.