温馨提示×

c++怎么获取文件大小

c++
小亿
116
2023-11-14 17:35:45
栏目: 编程语言

要获取文件大小,可以使用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或更高版本的编译器和库支持。

0