std::experimental::filesystem:: file_size
From cppreference.net
<
cpp
|
experimental
|
fs
|
定义于头文件
<experimental/filesystem>
|
||
|
std::
uintmax_t
file_size
(
const
path
&
p
)
;
std:: uintmax_t file_size ( const path & p, error_code & ec ) ; |
(1) | (文件系统 TS) |
返回常规文件
p
的大小,该大小通过读取 POSIX
stat
函数所获结构体的
st_size
成员确定(符号链接会被跟踪)。
尝试确定目录(以及任何其他非普通文件或符号链接的文件)的大小将被视为错误。
非抛出重载在出错时返回 - 1 。
目录 |
参数
| p | - | 要检查的路径 |
| 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::current_path() / "example.bin"; std::ofstream(p).put('a'); // 创建大小为1的文件 std::cout << "File size = " << fs::file_size(p) << '\n'; fs::remove(p); try { fs::file_size("/dev"); // 尝试获取目录大小 } catch (fs::filesystem_error& e) { std::cout << e.what() << '\n'; } }
可能的输出:
File size = 1 filesystem error: cannot get file size: Is a directory [/dev]
另请参阅
|
通过截断或零填充更改常规文件的大小
(函数) |
|
|
确定文件系统上的可用空闲空间
(函数) |