// Phil Ottewell's STL Course - http://www.pottsoft.com/home/stl/stl.htmlx // // Example 6.1 © Phil Ottewell 1997 // // Purpose: // Demonstrate input and output iterators from and to a file // ANSI C Headers #include // C++ STL Headers #include #include #include #include #ifdef _WIN32 using namespace std; #endif int main( int argc, char *argv[] ) { int i, iarray[] = { 1,3,5,7,11,13,17,19 }; fstream my_file("vector.dat",ios::out);// Add |ios::nocreate to avoid // creation if it doesn't exist vector v1, v2; for (i = 0;i(my_file," ")); cout << "Wrote vector v1 to file vector.dat" << endl; // Close file my_file.close(); // Open file for reading or writing my_file.open( "vector.dat", ios::in|ios::out ); // Read v2 from file copy( istream_iterator(my_file), // Start of my_file istream_iterator(), // Val. returned at eof inserter(v2,v2.begin())); cout << "Read vector v2 from file vector.dat" << endl; for ( vector::const_iterator iv=v2.begin(); iv != v2.end(); ++iv ) cout << *iv << " "; cout << endl; return( EXIT_SUCCESS ); }