4

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.

jww
  • 97,681
  • 90
  • 411
  • 885
hetelek
  • 3,776
  • 5
  • 35
  • 56
  • This would cause file fragmentation which you would have to clean up but you could just over-write the end of the files with EOF characters? – Precastic Jun 19 '13 at 02:53
  • What's the bigger picture here? "Resize" has many meanings! – Mahmoud Al-Qudsi Jun 19 '13 at 03:00
  • Is invoking a OS API allowed in your project? Linux has a system call `truncate` setting files to specified length. I think Windows will provide comparative API. BTW, what's your platform? – Summer_More_More_Tea Jun 19 '13 at 03:07
  • `truncate()` in `` is part of the POSIX C libraries, would that work? – trutheality Jun 19 '13 at 03:08
  • @Summer_More_More_Tea Invoking an OS API is okay, but this is a cross platform project. As long as I have a solution for every platform, I can just use pre-processors. – hetelek Jun 19 '13 at 03:09
  • @hetelek as @trutheality pointed out, `truncate` is a POSIX API. So cross-platform is not that much a problem, only if your OS API compatible with POSIX standard. Good luck! – Summer_More_More_Tea Jun 19 '13 at 06:22

4 Answers4

8

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

Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
  • Thanks for this, but unfortunately my project does not rely on Boost and I would like to keep it this way. – hetelek Jun 19 '13 at 02:57
  • 1
    @hetelek Why not? You may not use everything it offers, but you'll sure find some parts of it that may save much of your development time. – Mark Garcia Jun 19 '13 at 02:59
  • @MarkGarcia I have nothing against Boost, but I have a large project that doesn't rely on boost anywhere else, and I will not use it just for this without going back and changing them all. Also, I simply enjoy the fact that it only relies on standard libraries. – hetelek Jun 19 '13 at 03:03
  • 4
    @hetelek Then simply reimplement boost filesystem in your project. It is open source, the code is right there. Start typing... – Yakk - Adam Nevraumont Jun 19 '13 at 03:04
  • 3
    If you are using Visual Studio 11, the `` header is already there. I'm sure other major vendors will shortly follow suit. – Benjamin Lindley Jun 19 '13 at 03:07
6

C++17 has std::filesystem::resize_file()

Sid S
  • 6,037
  • 2
  • 18
  • 24
1

Indeed, there is no cross-platform method for resizing the file, because the Standard does not describe this functionality. But, as it is said here, there are platform APIs for this purpose: see _chsize() function for Windows, truncate() and ftruncate() function for POSIX systems.

Community
  • 1
  • 1
-1

File resize without loading full image is pretty complex task even for higher level languages, it’s mostly a feature of image processing SDKs like Image Magick.

If there are no specific requirements for resize quality or supported formats I guess Mark’s answer will be the easiest way. Otherwise you need to include graphics format libraries and implement some resize algorithm manually (or use some lib too).

Eugene
  • 3,335
  • 3
  • 36
  • 44