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

where are you outputting the data to the file? i have never seen it done with using the iterator normally in the while loop you should have a line like

// in while loop
fout << v1[i] << "\n";
i++;

where are you outputting the data to the file? i have never seen it done with using the iterator normally in the while loop you should have a line like

// in while loop
fout << v1[i] << "\n";
i++;

Hi NathanOliver,
Thanks for the interest shown.
Statement on line number 14

*out_it = *p;

writes contents of vector v1 to the file.
The problem begins from line 24 onwards wherein I am not able to read the file back using istream_iterator.

Check whether your stream is open using is_open().
Use eos as the exit criteria for the istream_iterator.

Nathan's version: fout << v1[i] << "\n"; mrinal.s2008's version: *out_it = *p; Are they similar?
Do both forms work?

Good catch WaltP.

@mrinal.s2008

Please check up the usage of ostream_iterator's * and ++ operators and how to add elements.

@thomas_naveen:
I tested is_open. Its open.

@WaltP
*out_it = *p;
this statementi is writing the content of the vector to the file.
I could see that the numbers from vector v1 are written to the file. (if I remove the binary mode, I could see the contents of file)
--------------

The problem begins with line 24 where I create in_it iterator.
the eof() call before line 24 returns 0 and as soon as I create istream_iterator, eof() returns true, which makes reading impossible. Since -- operator is not defined for istream_iterator, cannot traverse back.

I'm trying to resolve the question How to avoid this eof() problem.

I think this might be related to the file's content. Try saving the following numbers to your file (with e.g. Notepad) and see if it makes a difference.

1 2 3

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.