Jump to content
  • Advertisement
Sign in to follow this  
JohnHurt

sending std::vector to file

This topic is 4859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
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.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!