sending std::vector to file

Started by
5 comments, last by Rhaal 19 years ago
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().
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.
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, " "));}
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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.
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 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.
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.
- A momentary maniac with casual delusions.

This topic is closed to new replies.

Advertisement