Namespaces
Variants

std::experimental::filesystem:: file_size

From cppreference.net
定義先ヘッダ <experimental/filesystem>
std:: uintmax_t file_size ( const path & p ) ;
std:: uintmax_t file_size ( const path & p, error_code & ec ) ;
(1) (filesystem 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'); // 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';
    }        
}

出力例:

File size = 1
filesystem error: cannot get file size: Is a directory [/dev]

関連項目

通常ファイルのサイズを切り詰めまたはゼロ埋めによって変更する
(関数)
ファイルシステム上の利用可能な空き容量を決定する
(関数)