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

std::filesystem::file_size

Defined in header <filesystem>

std::uintmax_t file_size( const std::filesystem::path& p ); std::uintmax_t file_size( const std::filesystem::path& p, std::error_code& ec );

(1)

(since C++17)

返回常规文件的大小。p,就像通过阅读st_sizePOSIX结构成员统计%28符号链接跟随%29。

试图确定目录%28以及任何其他非常规文件或符号链接%29的文件的大小将被视为错误。

非抛出重载返回。-1关于错误。

参数

p

-

path to examine

ec

-

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

返回值

文件的大小,以字节为单位。

例外

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

noexcept规格:

noexcept

二次

代码语言:javascript
复制
#include <iostream>
#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
    fs::path p = fs::current_path() / "example.bin";
    std::ofstream(p).put('a'); // create file of size 1
    std::cout << "File size = " << fs::file_size(p) << '\n';
    fs::remove(p);
 
    try {
        fs::file_size("/dev"); // attempt to get size of a directory
    } catch(fs::filesystem_error& e) {
        std::cout << e.what() << '\n';
    }        
}

二次

可能的产出:

二次

代码语言:javascript
复制
File size = 1
boost::filesystem::file_size: Operation not permitted: "/dev"

二次

另见

resize_file (C++17)

changes the size of a regular file by truncation or zero-fill (function)

space (C++17)

determines available free space on the file system (function)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券