std::ostrstream is not buffering data properly

Mar 30, 2010 at 7:22am
I am using following piece of trouble code on my project. I have few problems with this..

code is:

{
std::ostrstream m_testBuffer;

m_testBuffer << "test Number " ;
m_testBuffer << 10;
m_testBuffer << "\n";
m_testBuffer << "test Start : " ;

std::cout << m_testBuffer .str() << endl; // --> 1
//m_testBuffer .clear();
//m_testBuffer .flush();

if(m_testBuffer .fail())
std::cout << "*** Previous operation failed" << endl;
if(m_testBuffer .bad())
std::cout << "*** bad state " << endl;

m_testBuffer << "test End " ;
m_testBuffer << 10 << "\n";
std::cout << m_testBuffer .str() << endl; // --> 2
}

OUTPUT :

test Number10
test Start : IIIIIIIIIIyyyy
*** Previous operation failed
test Number10
test Start : IIIIIIIIIIyyyy
======================================================
I have following problems with the above.

Prob 1. Data which i insert after the first cout are NOT getting inserted into the stream. why the second cout is also giving the same output.(or why thrwing "*** Previous operation failed" message.)

Prob 2. I tried to clear the content of the buffer (see the commented code)
//m_testBuffer .flush(); I believe it won't clear the content of the buffer. How to clear buffer content.. and start with a fresh stream.

Prob 3. why it is showing junk values on display.

Kindly post your suggestion or solution for these problems..

Warm Regards,
Mar 30, 2010 at 8:54am
Lukily, I have got an answer for the
>> Prob 1. Data which i insert after the first cout are NOT getting inserted into the stream. why the second cout is also giving the same output.(or why thrwing "*** Previous operation failed" message.)

std::cout << m_testBuffer .str() << endl;

m_testBuffer.str() function will make the stream to freeze().
We have to call m_testBuffer.freeze(false) after the first cout statement.

Prob 2 still remains.
Any one knows how to erase the contents of ostrstream ?.
Mar 30, 2010 at 9:09am
Can you please use the code tags? I mean this: [ code] here your code [/ code] just without whitespace in the tags.

Next one, please post the code in more context, what use clauses do you have and what headers did you include?

this code did work, please test this. Maybe your "endl" did some junk, because its not known in your scope? (watch for std::endl)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <sstream>
#include <iostream>

int main()
{
    std::ostringstream sout;
    
    sout << "test Number " << 10 << "\n" << "test Start : ";
    std::cout << sout.str() << std::endl;
    
    if (sout.fail())
        std::cout << "*** Previous operation failed" << std::endl;
    if (sout.bad())
        std::cout << "*** bad state " << std::endl;
        
    sout << "test End " << 10 << "\n";
    std::cout << sout.str() << std:: endl;
}
test Number 10
test Start :
test Number 10
test Start : test End 10
Mar 30, 2010 at 9:57am
Two issues with your code:

your 1) and 2): ostrstream freezes the buffer once you call .str(). You have to unfreeze it after each str() by using "m_testBuffer.freeze(false);"

your 3) the .str() of ostrstream does not add any null-termination to the string. cout << std::string need the null-termination. The additional garbage is what happens to be in the memory until you accidently hit a \0 somewhere.


Why do you use ostrstream? I found stringstream always superiour (like: non of your issues occur in stringstream ;).


Ciao, Imi.
Edit: Oh, and what maikel said: Please use code - tags. It's the "<>" button. Much easier to read.. ;)
Last edited on Mar 30, 2010 at 9:57am
Mar 30, 2010 at 10:11am
Imi:
Why does my code work? I have to check your statements :-( I did not know that problems!

Thanks,
Maikel

Edit:
oups. I did read stringstream, and not strstream. :-)
Last edited on Mar 30, 2010 at 10:23am
Mar 30, 2010 at 12:24pm
oups. I did read stringstream, and not strstream. :-)

Yep. That's why I didn't repeated your code but just summarized. I also use <sstream> and stringstream (or ostringstream), as all those "freezing/unfreezing" and incompability with std::string is just too bothersome. (strstream.str() returns char*, not std::string)

Ciao, Imi.
Topic archived. No new replies allowed.