Interleaving iostream read and write

G

Garrett Kajmowicz

I have two implementations of stringstream and they both handle
interleaved reads and writes differently. I was hoping that you might be
able to shed some light as to the "correct" operation, and hopefully point
out in the standard where this mode of operation is specified.

Here is the test code:

std::stringstream a;
float f;
int i;
std::string s;

a.str("2.35 5 Test");

a >> f >> i >> s;

std::cout << "f (should be 2.35): " << f << std::endl;
std::cout << "i (should be 5): " << i << std::endl;
std::cout << "s (should be Test): " << s << std::endl;

a.clear();
std::cout << "Buffer after flag clear: " << a.str() << std::endl;

std::cout <<"Combining read and write activities\n";
a.str("Testing data");
s = "";
a << 2.5 << " ";
a >> s;
std::cout << "Read out: " << "'" << s << "'" << std::endl;
a >> s;
std::cout << "Read out: " << "'" << s << "'" << std::endl;
std::cout << "Current buffer value: " << a.str() << std::endl;


The first sample output I get is:
f (should be 2.35): 2.35
i (should be 5): 5
s (should be Test): Test
Buffer after flag clear: 2.35 5 Test
Combining read and write activities
Read out: 'Testing'
Read out: 'data2.5'
Current buffer value: Testing data2.5

The second sample output I get is:
f (should be 2.35): 2.35
i (should be 5): 5
s (should be Test): Test
Buffer after flag clear: 2.35 5 Test
Combining read and write activities
Read out: '2.5'
Read out: 'ing'
Current buffer value: 2.5 ing data

Thanks for the info as to the correct operation.

- Garrett Kajmowicz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top