Original Post
Is the following code the shortest way to load a file into an std::vector with a C++ fstream?
void loadFile(const std::string& filename, std::vector<unsigned char>& buffer)
{
std::ifstream file(filename.c_str(), std::ios::in|std::ios::binary|std::ios::ate);
//get filesize
file.seekg(0, std::ios::end);
int size = file.tellg();
file.seekg(0, std::ios::beg);
size -= file.tellg();
//read contents of the file into the vector
buffer.resize(size);
file.read((char*)(&buffer[0]), size);
file.close();
}
I'm a bit worried that the get filesize stuff is pretty clumsy. Is it?