How can I get a file's size in C++?

C++Server Side ProgrammingProgramming

To get a file’s size in C++ first open the file and seek it to the end. tell() will tell us the current position of the stream, which will be the number of bytes in the file.

Example

 Live Demo

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

Output

Size of the file is 44 bytes
raja
Published on 18-Jun-2019 11:55:13
Advertisements