Sophie Sperner Sophie Sperner - 6 days ago 9
C++ Question

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

Let's create a complementary question to this one.
What is the most common way to get the file size in C++?
Before answering, make sure it is portable (may be executed on Unix, Mac and Windows),
reliable, easy to understand and without library dependencies (no boost or qt, but for instance glib is ok since it is portable library).

Answer
#include <fstream>

std::ifstream::pos_type filesize(const char* filename)
{
    std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
    return in.tellg(); 
}

See http://www.cplusplus.com/doc/tutorial/files/ for more information on files in C++.