N00B. Can I buffer cout, cout something and then cout the buffer? I've been googleing but did not find anything of this sort.

#include <iostream>

using namespace std;

int main (void)
{
	cout << "cout output until this point";

	string BUFFER; // I need to buffer the cout output until now.

	cout << "Content-length: " + BUFFER.length(); // cout this first.

	cout << BUFFER; // cout the BUFFER after the: Content-length.

	cout.flush(); // Is this necessary?

	return 0;
}

Recommended Answers

All 5 Replies

"Content-length: "
That is a string and this:
BUFFER.length()
equates to an int. So when you use the + operator between the two are you trying to concatenate an int onto the string or are you trying to add an int to a string or are what are you trying to do? You could output both like this:

cout << "Content-length: ";
cout << BUFFER.length();

but everything between the << and ; operators in this line:

cout << "Content-length: " + BUFFER.length();

is considered one object so it has to be compatible one way or the other.

"Content-length: "
That is a string and this:
BUFFER.length()
equates to an int.

Yes, I made a mistake by concatenating a string and an integer when I drew up that example.

But how can I buffer cout and modify the order they are output?

Try another example so we can understand better what you are trying to do.

This:
cout << 4 + 7;
works fine as does this:
string word1 = "hello";
string word2 = " world";
cout << word1 + word2;

but I'm not sure what you are thinking about.

-

Try another example so we can understand better what you are trying to do.

1. How can I store all the output into a buffer?
2. Then, I have to get the size of that buffer and cout a string stating that buffer's size. How can I achieve this?
3. Then, cout that buffer's content.

#include <iostream>

using namespace std;

int main (void)
{
	cout << "cout output until this point";

	string BUFFER; // I need to buffer the cout output until now.

	cout << "Content-length: " << BUFFER.length(); // Then, cout the size of the buffer.

	cout << BUFFER; // Then, cout that buffer's content.

	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.