[C++] ファイルサイズの取得

ディスク上のファイルのファイルサイズを取得するサンプルコードです。

#include <iostream>
#include <fstream>

int main () {
    
    std::ifstream ifs("/var/tmp/data.xml");
    ifs.seekg(0, std::ios::end);
    std::ifstream::pos_type end = ifs.tellg();
    std::cout
        << std::to_string(end)
        << " bytes "
        << std::endl;
    return 0;
}

出力結果

$ ./GetFileSize
5976576 bytes 

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です