CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    335

    Reusing std::stringstream after EOF

    Hi all,

    I´m using a std::stringstream and filling it with several lines. I need to print these lines 2 times and I´m not getting to use it at the second time. I presume that the eofbit is set after the first use so I need to clear it before any other operation. The problem is that even if I clear the bit ( If I´m doing it correctly) the seek(0) dosen´t work, for exemple:
    Code:
     std::stringstream strtest;
    
        strtest << "aaa bbb cccc" << std::endl;
        strtest << "ddd eee ffff" << std::endl;
        strtest << "ggg hhh iiii" << std::endl;
    
        std::string teste;
    
       //---- if I comment 2 of these extrations lines the seek(0) works ok
       //---- (Because the EOF is not reached....)
        
        strtest >> teste;
        strtest >> teste;
        strtest >> teste; 
        strtest >> teste;                
        strtest >> teste; 
        strtest >> teste;
        strtest >> teste;
        strtest >> teste;
        strtest >> teste;
        strtest >> teste;
    
        strtest.unsetf(std::ios::eofbit);
        strtest.unsetf(std::ios::failbit);       //--- for safe
        strtest.unsetf(std::ios::badbit);     //---     " 
    
        strtest.seekg(0);
    
        //--- second extration (dosen´t work....)
    
        strtest >> teste;
        strtest >> teste;
        strtest >> teste; 
        strtest >> teste; 
        strtest >> teste; 
        strtest >> teste;
        strtest >> teste;
        strtest >> teste;
        strtest >> teste;
    Can anyone tell me what is wrong???

    thank you in advance.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Reusing std::stringstream after EOF

    I don't think that you can use those constants with unsetf (which
    expects format flags , not io state flags).

    Just use clear() before the seekg ...

    Code:
    strtest.clear();
    
    strtest.seekg(0);

  3. #3
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    335

    Re: Reusing std::stringstream after EOF

    Ok Philip, is it! Thank you very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured