如何在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