Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing a file in C++

Tags:

c++

file

resize

I am surprised to say that I have not found a reasonable solution on how to resize a file using C++ standard libraries. It seems completely stupid and wasteful to have to read in the data from the original file, and then write it into a different file. I simply want a resize method that will chop off the data that goes beyond that size... Is there anything that does this?

EDIT: If I do have to use the method I explained above, how large should my buffer be that I'm using to read/write? I obviously can't read the whole file in at once, because I am working with rather large files here.

like image 256
hetelek Avatar asked Nov 14 '25 09:11

hetelek


1 Answers

You can use the functions provided by Boost.Filesystem, specifically its resize_file() function.

Please note that Boost.Filesystem is (planned to be) included in future iterations of the language standard. See here. That's good news if you don't want to use Boost in your project.

Also, as what @BenjaminLindley had mentioned, the <filesytem> header is already available in Visual Studio 2012 (see here; search for "filesystem").

like image 56
Mark Garcia Avatar answered Nov 17 '25 08:11

Mark Garcia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!