首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::filesystem::resize_file

Defined in header <filesystem>

?

?

void resize_file(const std::filesystem::path& p, std::uintmax_t new_size); void resize_file(const std::filesystem::path& p, std::uintmax_t new_size, std::error_code& ec);

?

(since C++17)

更改指定的常规文件的大小。p好像是由POSIX截尾*如果文件大小以前大于new_size文件的其余部分被丢弃。如果文件以前小于new_size,文件大小增加,新区域显示为零填充。

参数

p

-

path to resize

new_size

-

size that the file will now have

ec

-

out-parameter for error reporting in the non-throwing overload

返回值

%280%29

例外

不占用std::error_code&参数抛文件系统[医]误差关于基础OS API错误,使用p作为第一个参数和操作系统错误代码作为错误代码参数。std::bad_alloc如果内存分配失败,则可能引发。过载std::error_code&参数,如果OSAPI调用失败,则将其设置为OSAPI错误代码,并执行ec.clear()如果没有错误发生。这个过载

noexcept规格:

noexcept

注记

在支持稀疏文件的系统上,增加文件大小并不会增加它在文件系统上所占的空间:只有当非零字节被写入文件时,才会进行空间分配。

演示在空闲空间上创建稀疏文件的效果。

二次

代码语言:javascript
复制
#include <iostream>
#include <iomanip>
#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
    fs::path p = fs::current_path() / "example.bin";
    std::ofstream(p).put('a');
    std::cout << "File size: " << std::setw(10) << fs::file_size(p)
              << " Free space: " << fs::space(p).free << '\n';
    fs::resize_file(p, 1024*1024*1024); // resize to 1 G
    std::cout << "File size: " << fs::file_size(p)
              << " Free space: " << fs::space(p).free << '\n';
    fs::remove(p);
}

二次

可能的产出:

二次

代码语言:javascript
复制
File size:          1 Free space: 3724541952
File size: 1073741824 Free space: 3724476416

二次

另见

file_size (C++17)

returns the size of a file (function)

space (C++17)

determines available free space on the file system (function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com