Get the Size of a File in C++
I've sometimes had the need to find the size or length of a file in C++. It's pretty easy, but I figured someone out there could make use of my function. I'm just nice like that.
#include <fstream>
int getFileSize(const std::string &fileName)
{
ifstream file(fileName.c_str(), ifstream::in | ifstream::binary);
if(!file.is_open())
{
return -1;
}
file.seekg(0, ios::end);
int fileSize = file.tellg();
file.close();
return fileSize;
}
-1
if the file could not be opened.