Load binary file using fstream
- by Kirill V. Lyadvinsky
I'm trying to load binary file using fstream in the following way:
#include <iostream
#include <fstream
#include <iterator
#include <vector
using namespace std;
int main()
{
basic_fstream<uint32_t file( "somefile.dat", ios::in|ios::binary );
vector<uint32_t buffer;
buffer.assign( istream_iterator<uint32_t, uint32_t( file ), istream_iterator<uint32_t, uint32_t() );
cout << buffer.size() << endl;
return 0;
}
But it doesn't work. In Ubuntu it crashed with std::bad_cast exception. In MSVC++ 2008 it just prints 0.
I know that I could use file.read to load file, but I want to use iterator and operator>> to load parts of the file. Is that possible? Why the code above doesn't work?