L o a d i n g . . .
主打一个C++
文章详情

C++通过文件路径获取文件大小

Posted on 2022-02-28 18:38:11 by 主打一个C++

#include <iostream>
#include <fstream>

long long getFileSize(const std::string& filePath) {
    std::ifstream file(filePath, std::ios::binary | std::ios::ate);
    if (!file) {
        std::cerr << "无法打开文件:" << filePath << std::endl;
        return -1;
    }
    std::streampos size = file.tellg();
    file.close();
    return size;
}

//调用
int main() {
    std::string filePath = "your_file_path_here";
    long long fileSize = getFileSize(filePath);
    if (fileSize!= -1) {
        std::cout << "文件大小为:" << fileSize << " 字节。" << std::endl;
    }
    return 0;
}


*转载请注明出处:原文链接:https://cpp.vin/page/49.html

作者近期文章
  • 随手笔记
  • 主打一个C++   2025-01-11 20:02:01
  • 都2000000025年了。还有不能随意访问guthub的,仔细看。在国内其实是可以正常访问的,gfw并没屏蔽。这里给出其中一个简单直接的方法稳定访问。1. 随便百度一个”dn
提示
×
确定
数据库执行: 8次 总耗时: 0.01s
页面加载耗时: 



wechat +447752296473
wechat cpp-blog