Using C++ filestreams (fstream), how can you determine the size of a file?

warren picture warren · Mar 9, 2010 · Viewed 159.6k times · Source

I'm sure I've just missed this in the manual, but how do you determine the size of a file (in bytes) using C++'s istream class from the fstream header?

Answer

Pepus picture Pepus · Nov 15, 2012

You can open the file using the ios::ate flag (and ios::binary flag), so the tellg() function will give you directly the file size:

ifstream file( "example.txt", ios::binary | ios::ate);
return file.tellg();