如何使用 C++17 filesystem 库获取文件大小
要仅使用 C++17 filesystem 库获取任何文件(在我们的示例中为 test.xml)的文件大小(以字节为单位),使用此代码片段:
使用 filesystem 的新版本:
get_file_size.cpp
#include <filesystem>
#include <iostream>
using namespace std;
using namespace std::filesystem;
int main() {
size_t filesize = file_size("test.xml");
cout << filesize << endl;
}使用 experimental/filesystem 的旧版本
get_file_size_experimental.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;
}使用 GCC/G++ 你需要链接 stdc++fs 库,即像这样编译:
compile_get_file_size.sh
g++ -o test test.cpp -lstdc++fsCheck out similar posts by category:
C/C++
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow