如何使用 boost::filesystem 库获取文件大小

要使用 boost::filesystem 库获取任何文件(在我们的示例中为 test.xml)的文件大小(以字节为单位),使用此代码片段:

filesize-example.cpp
#include <experimental/filesystem>
#include <iostream>

using namespace std;
using namespace std::experimental::filesystem;

int main() {
    size_t filesize = file_size("test.xml");
    cout << filesize << endl;
}

你需要链接 boost_filesystemboost_system 库,即像这样编译:

build-filesize.sh
g++ -o test test.cpp -lboost_filesystem -lboost_system

Check out similar posts by category: C/C++