c++

c++怎么获取文件大小

小亿
207
2023-11-14 17:35:45
栏目: 编程语言
C++开发者专用服务器,限时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元/月。点击查看>>

相关推荐:php怎么获取文件大小

0
看了该问题的人还看了