Computer >> Máy Tính >  >> Lập trình >> C ++

Làm cách nào để lấy kích thước tệp trong C ++?

Để có kích thước của tệp trong C ++, trước tiên hãy mở tệp và tìm kiếm nó ở cuối. Tell () sẽ cho chúng ta biết vị trí hiện tại của luồng, đó sẽ là số byte trong tệp.

Ví dụ

#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";
}

Đầu ra

Size of the file is 44 bytes