要获取文件大小,可以使用std::filesystem库中的file_size函数。以下是一个示例:
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
    fs::path filePath = "example.txt";
    
    try {
        uintmax_t fileSize = fs::file_size(filePath);
        std::cout << "File size: " << fileSize << " bytes" << std::endl;
    } catch (const fs::filesystem_error& ex) {
        std::cout << "Error: " << ex.what() << std::endl;
    }
    
    return 0;
}
在上面的示例中,我们使用std::filesystem库中的file_size函数获取文件的大小,并将其打印出来。请注意,使用std::filesystem需要C++17或更高版本的编译器和库支持。