2

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.

Santosh Kale
  • 113
  • 12

2 Answers2

7

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.

mvp
  • 111,019
  • 13
  • 122
  • 148
2

You can use the resize_file() function from the <filesystem> addition to the Standard Library in C++17:

#include <filesystem>
namespace fs = std::filesystem;

// ...

fs::resize_file(p, 1024); // resize to 1024 bytes
Galik
  • 47,303
  • 4
  • 80
  • 117