Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Lấy kích thước file trong C++?

Để lấy kích thước file trong C++, mở file và seek đến cuối. Hàm tell() sẽ cho biết vị trí hiện tại của stream, chính là số byte trong file.

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";
  return 0;
}

Kết quả

Size of the file is 44 bytes