std::filesystem:: resize_file
|
定义于头文件
<filesystem>
|
||
|
void
resize_file
(
const
std::
filesystem
::
path
&
p,
std:: uintmax_t new_size ) ; |
(1) | (C++17 起) |
|
void
resize_file
(
const
std::
filesystem
::
path
&
p,
std::
uintmax_t
new_size,
|
(2) | (C++17 起) |
更改由
p
指定的常规文件大小,其行为类似于 POSIX
truncate
:若文件原尺寸大于
new_size
,则超出部分将被截断丢弃;若文件原尺寸小于
new_size
,则文件尺寸将扩展,新增区域呈现为零填充状态。
目录 |
参数
| p | - | 要调整大小的路径 |
| new_size | - | 文件现在将具有的大小 |
| ec | - | 非抛出重载中用于错误报告的输出参数 |
返回值
(无)
异常
任何未标记为
noexcept
的重载在内存分配失败时可能抛出
std::bad_alloc
。
注释
在支持稀疏文件的系统上,增加文件大小不会增加其在文件系统上占用的空间:仅当向文件写入非零字节时才会进行空间分配。
示例
演示创建稀疏文件对可用空间的影响。
#include <filesystem> #include <fstream> #include <iostream> #include <locale> int main() { auto p = std::filesystem::temp_directory_path() / "example.bin"; std::ofstream{p}.put('a'); std::cout.imbue(std::locale{"en_US.UTF8"}); std::cout << "File size: " << std::filesystem::file_size(p) << '\n' << "Free space: " << std::filesystem::space(p).free << '\n'; std::filesystem::resize_file(p, 64*1024); // resize to 64 KB std::cout << "File size: " << std::filesystem::file_size(p) << '\n' << "Free space: " << std::filesystem::space(p).free << '\n'; std::filesystem::remove(p); }
可能的输出:
File size: 1 Free space: 42,954,108,928 File size: 65,536 Free space: 42,954,108,928
参见
|
(C++17)
|
返回文件大小
(函数) |
|
(C++17)
|
确定文件系统上的可用空闲空间
(函数) |