Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Interleaving iostream read and write (http://www.velocityreviews.com/forums/t447697-interleaving-iostream-read-and-write.html)

Garrett Kajmowicz 08-13-2005 11:54 PM

Interleaving iostream read and write
 
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


All times are GMT. The time now is 01:54 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.