warren warren - 4 years ago 156
C++ Question

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

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 Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download