• ### Popular Now

• 15
• 15
• 11
• 9
• 10

#### Archived

This topic is now archived and is closed to further replies.

# C++ Stream Buffer

This topic is 6454 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m writing text to the screen using cout << The problem is that sometimes halfway writing an error occurs, and the previous text already written must be deleted and replaced by a nice error message. The solution is to buffer all text in a temp io buffer, clearing this buffer on errors. How do I create such a buffer, and how do I move data between this buffer and the cout? Extra info: - I don''t want to create temp files!!! It''s multi-user so this will slow it down and cause extra problems! - It must be for both Windows and Linux, using standard C++ iostream. This is a difficult problem, and I bet almost nobody can answer this one - so I need YOUR post...

##### Share on other sites
I hoped I was wrong...

##### Share on other sites
Use string streams. They are a special kind of stream using a memory buffer. Like this:

  #include #include using namespace std;int main() { strstream str; // Replace this if you want to change your means of output ostream& output = str; output << "Executing critical function now..."; // Execute critical func here int code = CriticalFunc(); if (code < 0) { // Reset output stream output.clear(); output.seekp(0); // Write to output output << "Error executing critical function" << '\0' << endl; } else { output << "success" << '\0'; } cout << output.str() << endl; return 0;}

For some reason, you have to append a '\0' character at the end or you will get all sorts of garbage in your input stream. Don't know if this is a bug or a 'feature' .

Also, I'm not sure if replacing the "ostream" with, say, cout, will work. I guess I should test this, but I'm too lazy .

Hope this helps,
Dormeur

Edited by - dormeur on July 19, 2000 4:10:21 AM

##### Share on other sites
Domeur, what compiler are you using? It seems to me that the need for ''\0'' is a bug in the C++ library that comes with your compiler? Rogue Wave''s implementation (that comes with Borland C++ 5.5) has some bugs for sure, and Visual C++''s implementation isn''t bug free either...

Erik

##### Share on other sites
I''m using VC++ 6.0, although I sometimes use the gcc compiler in Linux (don''t know the version). But I can''t remember ever having used stringstreams in Linux, so I''m still not sure if the ''\0'' is a bug or a feature. (But if you say it''s a bug, Erik, and since it''s written by M$, I believe you .) On the other hand, I can think of some cases where having to append the ''\0'' might be useful. Suppose the strstream automatically appends the null terminator each time I write a string to it, it''ll print only the first string written to it. Like this:   strstream str;int a = 10;str << "The value of a is " << a << ".";  This code will only print "The value of a is", if the "strstream::operator <<" automatically appends ''\0'' to each string. Still, I think it''s a bug in the strstream::str() method. It should append a null terminator in the return string. Hey Micro$oft guys, are you reading this?

Dormeur

##### Share on other sites
quote:
Original post by Dormeur
Still, I think it''s a bug in the strstream::str() method. It should append a null terminator in the return string. Hey Micro\$oft guys, are you reading this?

It''s not a bug.. you need to end the string yrself with ends like

  str << "Here " << a << ends;

##### Share on other sites
I seem to recall that strstream is defined as a "C"-style string buffer, thus requiring a null-termination, and sstream is a std::string buffer.

##### Share on other sites
Thanks a lot! So there really are people who can answer this

It looks like a nice method.
Will start implementation in a few minutes...

I been having problems with this for weeks, so thanks again