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.
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.
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.
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