Sign in to follow this  
_Camus_

Read from binary to a std::vector

Recommended Posts

Hi, normally i use to write some files to a binary files on this way: struct Ex { int a; float b; bool c; }; //... Ex obj; obj.a = 10; obj.b = 5.0f; c = true; ofstream Files("RP.dat",ios::binary | ios::app | ios::out); Files.write(reinterpret_cast<char *>(&obj),sizeof(Ex)); Files.close(); And append new registers to the same file, when i want to open the file, my way to do it is: int size; int numRegisters; Ex *AllReports; ifstream file ("RP.dat", ios::in|ios::binary|ios::ate); size = file.tellg(); numRegisters = size/sizeof(Ex); AllReports = new Ex[numRegisters]; file.seekg (0, ios::beg); file.read (reinterpret_cast<char *>(&AllReports[0]), size ); file.close(); Its a very easy way to load to an array all the file, but i need to use a std::vector, i want to know if there is some way to do it in the same way, i can do it by the hard way with strides byte per byte, but i´m sure there is a better way to do it. Thanks

Share this post


Link to post
Share on other sites
std::vector holds elements as continuous memory block, same way normal array does, so you can read elements like this:

std::vector<Ex> AllReports(numRegisters);

file.read (reinterpret_cast<char*>(&AllReports[0]), size);

Share this post


Link to post
Share on other sites
Oh lool, that work´s perfect, i was even looking istream iterator,
but is so simple, is almost the same, i notice that a pointer to
the begin of the vector works too:

file.read (reinterpret_cast<char *>(&*Reports.begin()), size );

Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by _Camus_
Oh lool, that work´s perfect, i was even looking istream iterator,
but is so simple, is almost the same, i notice that a pointer to
the begin of the vector works too:

file.read (reinterpret_cast<char *>(&*Reports.begin()), size );

Thanks!


you are actually dereferencing iterator to the first element in the container. another popular way is

file.read (reinterpret_cast<char *>(&Reports.front()), size );

where front() returns reference to first element so that iterator indirection is avoided

Share this post


Link to post
Share on other sites
They will probably all compile to the same code, but I think an argument can be made that '&vec.front()' is the clearest and most idiomatic form (even though I would personally be most likely to write '&(vec[0])' ^^; ).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this