ios_base::beg 
Author Message
 ios_base::beg

I have some problem in using std::ios_base::beg with
fstreams.I have attached the code I tried with this.

    std::fstream test_out("open_test.txt",  
std::ios_base::out);
   test_out << "abcd";
   test_out.flush();
   test_out.close();

   // std::ios_base::in
   std::string output_buffer;
   std::fstream test_in("open_test.txt",
std::ios_base::in);

   test_in >> output_buffer;

   test_in.seekg(0,std::ios_base::beg);
   output_buffer.erase(output_buffer.begin(),
output_buffer.end());
   std::getline(test_in, output_buffer);  

output_buffer does not have any data when I read the
second time after making the pointer point to the
beginning of the file. Kindly tell me as to why this is
happening?

Thanks



Wed, 14 Dec 2005 20:06:34 GMT  
 ios_base::beg
Quote:
> I have some problem in using std::ios_base::beg with
> fstreams.I have attached the code I tried with this.

>     std::fstream test_out("open_test.txt",  
> std::ios_base::out);
>    test_out << "abcd"; <------- Change this
>    test_out.flush();
>    test_out.close();

>    // std::ios_base::in
>    std::string output_buffer;
>    std::fstream test_in("open_test.txt",
> std::ios_base::in);

>    test_in >> output_buffer; <------------ First read, sets EOF

>    test_in.seekg(0,std::ios_base::beg); <----- Resets file pointer but
does not delete EOF
>    output_buffer.erase(output_buffer.begin(),
> output_buffer.end());
>    std::getline(test_in, output_buffer);   <------------ Second read,

fails becouse of the EOF state

Quote:

> output_buffer does not have any data when I read the
> second time after making the pointer point to the
> beginning of the file. Kindly tell me as to why this is
> happening?

> Thanks

The problem you're seeing is that the with the first read you're reaching
EOF. The subsequent seekg does not reset that state so getline fails. You
can verify that that's the problem with your code by changing:
    test_out << "abcd";  
to this:
    test_out << "abcd" << endl;

To correct your code, you have to clear the error-state of you stream after
you've seeked it by:
    test_in.clear();

--
Andras Tantos, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights.



Sat, 24 Dec 2005 02:23:58 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. std::ios_base::show_pos

2. ios_base

3. More ADSI Blues..Begging for help...

4. Beg. Question

5. string beg. question

6. New to C - how to sub-string - I beg of thee for HELP

7. Beg. Probs

8. Beg. Question

9. Beg. Problem

10. beg question

11. conceptual question begging answer

12. Beg. Probs

 

 
Powered by phpBB® Forum Software