c++怎么获取文件大小

c++
1450
2023/11/19 17:35:45
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: c++中fixed的作用是什么