Hi,
In the code snippet below, I tried to write the contents of a vector to a file using ostream iterator.
However, when I try to read the contents of the file using istream_iterator, there is a problem. When the istream_iterator object is initialized using ifstream object fin, the istream_iterator points to EOF (-1), which makes further reading impossible.
Please help me in resolving this. Is there something else I have to use instead of istream_iterator?
ostream_iterator<int> out_it(fout);
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
v1.push_back(60);
vector<int> :: iterator p = v1.begin();
while(p != v1.end()) {
*out_it = *p;
p++;
out_it++;
}
fout.close();
ifstream fin(argv[1], ios::in | ios::binary);
cout << fin.eof() << endl;
istream_iterator<int> in_it(fin);
cout << *in_it << " " << fin.eof() << endl; // -1 1
cout << *in_it << endl;
vector<int> v2;
cout << "TRACE: before while loop" << endl;
while(fin.eof() != true) {
cout << "TRACE: while loop begins" << endl;
cout << *in_it << " " ;
v2.push_back(*in_it);
in_it++;
}
// nothing is stored in v2.
// code to print contents of v2 follws