quote:Original post by daerid
#include <iostream>#include <fstream>#include <string>size_t get_file_length(const std::string& filename){ std::ifstream file(filename.c_str()); std::string fcontents( std::istream_iterator<char>(file), std::istream_iterator<char>() ); return fcontents.length();}
EDIT: Note that this won''t work accurately for binary files.
Dumping it into a string is a waste of time and memory; std::distance() will tell you how far apart the iterators are without recording what''s in the file.
And using istream_iterators is wrong, as they''ll gobble up whitespace. istreambuf_iterators will count everything.