如何用 C++ 获取文件的大小?


要获取 C++ 中的文件大小,首先打开文件并将其查找至结尾。tell() 将告诉我们流的当前位置,该位置是文件中的字节数。

示例

 演示

#include<iostream>
#include<fstream>
using namespace std;
int main() {
   ifstream in_file("a.txt", ios::binary);
   in_file.seekg(0, ios::end);
   int file_size = in_file.tellg();
   cout<<"Size of the file is"<<" "<< file_size<<" "<<"bytes";
}

输出

Size of the file is 44 bytes

更新日期:2019 年 7 月 30 日

14K+ 次浏览

开启你的 职业生涯

完成课程以获得认证

开始
广告
© . All rights reserved.