std::experi...resize_file(3)
π std::experi...resize_file(3) π
okay so bestie, this is the C++ Standard Libary manual page β and yes, βLibaryβ is literally the spelling in the source, we are leaving it, no cap π
. The star of the show is std::experimental::filesystem::resize_file, and Iβm highkey obsessed with how specific this little function is.
πΈ NAME πΈ
std::experimental::filesystem::resize_file β and the pageβs short description is, iconically, the exact same thing again: std::experimental::filesystem::resize_file.
π¦ Synopsis π¦
Defined in header <experimental/filesystem> β¨
The signatures are right here, with the (filesystem TS) exactly where the page puts it:
void resize_file( const path& p, std::uintmax_t new_size );
void resize_file( const path& p, std::uintmax_t new_size,
(filesystem TS)
error_code& ec );
so what does it do? It changes the size of the regular file named by p as if by POSIX truncate: if the file size was previously larger than new_size, the remainder of the file is discarded. If the file was previously smaller than new_size, the file size is increased and the new area appears as if zero-filled. literally truncate behavior, bestie π«Ά.
π Parameters π
pβ path to resize.new_sizeβ size that the file will now have.ecβ out-parameter for error reporting in the non-throwing overload.
π Return value π
(none) β itβs giving void, bestie. No return value, periodt.
π₯ Exceptions π₯
- The overload that does not take an
error_code¶meter throwsfilesystem_erroron underlying OS API errors, constructed withpas the first argument and the OS error code as the error code argument.std::bad_allocmay be thrown if memory allocation fails. - The overload taking an
error_code¶meter sets it to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur. This overload hasnoexceptspecification:noexcept.
π¦ Notes π¦
On systems that support sparse files, increasing the file size does not increase the space it occupies on the file system: space allocation takes place only when non-zero bytes are written to the file. lowkey sparse-file magic, iykyk β¨
π Example π
Demonstrates the effect of creating a sparse file on the free space.
The page says // Run this code, so run it, bestie π:
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
fs::path p = fs::temp_directory_path() / "example.bin";
std::ofstream(p).put('a');
std::cout << "File size: " << fs::file_size(p) << '\n'
<< "Free space: " << fs::space(p).free << '\n';
fs::resize_file(p, 64*1024); // resize to 64 KB
std::cout << "File size: " << fs::file_size(p) << '\n'
<< "Free space: " << fs::space(p).free << '\n';
fs::remove(p);
}
Possible output:
File size: 1
Free space: 31805444096
File size: 65536
Free space: 31805444096
π See also π
file_sizeβ returns the size of a file (function).spaceβ determines available free space on the file system (function).
π Categories π
- Noindexed pages
- unconditionally noexcept
π§ Hidden categories π§
- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template
and thatβs the whole page, bestie π. The footer/source thing was http://cppreference.com, version/date 2024.06.10, and the footer title was std::experi...resize_file(3) β mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms