Sign in to follow this  
JohnHurt

sending std::vector to file

Recommended Posts

JohnHurt    334
I'm just starting out int STL and was wondering if it is possible to send a std::vector stream to a file. Then if you can read it back in in one go. something kind of like fwrite().

Share this post


Link to post
Share on other sites
Sneftel    1788
Well, yyyeah..... you can do funky-cool stuff with std::copy and std::ostream_iterator and std::istream_iterator. Really, though, the easiest way to do it is just in a for-loop.

Share this post


Link to post
Share on other sites
this should illustrate how to do it:

#include <iostream>
#include <iterator>
#include <vector>
#include <fstream>

int main(void)
{
std::vector<int> v, w;
for(int i = 0; i != 10; ++i)
v.push_back( i);
//output to file
{
std::ofstream out( "out.txt");
std::copy( v.begin(), v.end(), std::ostream_iterator<int>( out, "\n"));
}
//read from file
{
std::ifstream in( "out.txt");
std::copy( std::istream_iterator<int>( in), std::istream_iterator<int>(), std::back_inserter( w));
}
//output
std::copy( v.begin(), v.end(), std::ostream_iterator<int>( std::cout, " "));
endl( std::cout);
std::copy( w.begin(), w.end(), std::ostream_iterator<int>( std::cout, " "));
}

Share this post


Link to post
Share on other sites
JohnHurt    334
Ah yes, what I should have said is that I want it to be written out as a chunk of binary, so that i can read the vector straight back in in one lump. But i suppose, thinking about it, that it wouldn't work with something dynamic like a vector.

Share this post


Link to post
Share on other sites
DrEvil    1148
boost::serialization can serialize stl containers in and out of files with one line of code, even handles pointers and actually saves the data pointed to.

Share this post


Link to post
Share on other sites
try_catch_this    373
I think its best to provide a serial read and write to the class you store in the vector. As well as a way to know how many elements are in the file.


class myclass
{
read( std::ifstream& file )
{
file.read( (char*)i, sizeof(i) );
}
write( std::ofstream& file )
{
file.write( (char*)i, sizeof(i) );
}
int i;
}

void writing()
{
std::vector<myclass> vecmyclass;
std::ofstream file( "myclass.dat", std::ios::binary );
vecmyclass::size_type size = vecmyclass.size();
file.write( &size, sizeof(vecmyclass::size_type) );
for( vecmyclass::const_iterator i = vecmyclass.begin();
i<vecmyclass.end() ; ++i )
{
i.write( file );
}
}

void reading()
{
std::vector<myclass> vecmyclass;
std::ifstream file( "myclass.dat", std::ios::binary );
vecmyclass::size_type size;
file.read( &size, sizeof(vecmyclass::size_type) );
vecmyclass.resize( size );
for( vecmyclass::const_iterator i = vecmyclass.begin();
i<vecmyclass.end() ; ++i )
{
i.read( file );
}
}



For built in types like int just use the write method of the ofstream.

Share this post


Link to post
Share on other sites
Rhaal    754
Quote:
Original post by DrEvil
boost::serialization can serialize stl containers in and out of files with one line of code, even handles pointers and actually saves the data pointed to.


I'd like to second this, and boost is popular enough that you don't need to feel guilty using it's serialization, even professionally. Many boost features are even being added to the next C++ standard from what I hear.

Serialization with boost.

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