std::experimental::filesystem:: resize_file
From cppreference.net
<
cpp
|
experimental
|
fs
|
定义于头文件
<experimental/filesystem>
|
||
|
void
resize_file
(
const
path
&
p,
std::
uintmax_t
new_size
)
;
void resize_file ( const path & p, std:: uintmax_t new_size, error_code & ec ) ; |
(文件系统 TS) | |
更改由 p 命名的常规文件大小,其行为类似于 POSIX truncate 函数:若原文件大小超过 new_size ,则超出部分将被截断丢弃;若原文件大小小于 new_size ,则文件将被扩展至指定大小,新增区域呈现为零填充状态。
目录 |
参数
| p | - | 要调整大小的路径 |
| new_size | - | 文件现在将具有的大小 |
| ec | - | 非抛出重载中用于错误报告的输出参数 |
返回值
(无)
异常
The overload that does not take an error_code & parameter throws filesystem_error on underlying OS API errors, constructed with p as the first argument and the OS error code as the error code argument. std:: bad_alloc may be thrown if memory allocation fails. The overload taking an error_code & parameter sets it to the OS API error code if an OS API call fails, and executes ec. clear ( ) if no errors occur. This overload has
noexcept
规范:
noexcept
注释
在支持稀疏文件的系统上,增加文件大小不会增加其在文件系统上占用的空间:仅当向文件写入非零字节时才会进行空间分配。
示例
演示创建稀疏文件对可用空间的影响。
运行此代码
#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); }
可能的输出:
File size: 1 Free space: 31805444096 File size: 65536 Free space: 31805444096
参见
|
返回文件大小
(函数) |
|
|
确定文件系统上的可用空闲空间
(函数) |