buffer cout output and modify cout output order

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 21
Reputation: raigs is an unknown quantity at this point 
Solved Threads: 0
raigs raigs is offline Offline
Newbie Poster

buffer cout output and modify cout output order

 
0
  #1
Oct 23rd, 2009
N00B. Can I buffer cout, cout something and then cout the buffer? I've been googleing but did not find anything of this sort.
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main (void)
  6. {
  7. cout << "cout output until this point";
  8.  
  9. string BUFFER; // I need to buffer the cout output until now.
  10.  
  11. cout << "Content-length: " + BUFFER.length(); // cout this first.
  12.  
  13. cout << BUFFER; // cout the BUFFER after the: Content-length.
  14.  
  15. cout.flush(); // Is this necessary?
  16.  
  17. return 0;
  18. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso
 
0
  #2
Oct 23rd, 2009
"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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 21
Reputation: raigs is an unknown quantity at this point 
Solved Threads: 0
raigs raigs is offline Offline
Newbie Poster
 
0
  #3
Oct 23rd, 2009
Originally Posted by Lerner View Post
"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?
Last edited by raigs; Oct 23rd, 2009 at 5:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso
 
0
  #4
Oct 23rd, 2009
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 21
Reputation: raigs is an unknown quantity at this point 
Solved Threads: 0
raigs raigs is offline Offline
Newbie Poster
 
0
  #5
Oct 23rd, 2009
-
Last edited by raigs; Oct 23rd, 2009 at 7:08 pm. Reason: double post
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 21
Reputation: raigs is an unknown quantity at this point 
Solved Threads: 0
raigs raigs is offline Offline
Newbie Poster
 
0
  #6
Oct 23rd, 2009
Originally Posted by Lerner View Post
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.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main (void)
  6. {
  7. cout << "cout output until this point";
  8.  
  9. string BUFFER; // I need to buffer the cout output until now.
  10.  
  11. cout << "Content-length: " << BUFFER.length(); // Then, cout the size of the buffer.
  12.  
  13. cout << BUFFER; // Then, cout that buffer's content.
  14.  
  15. return 0;
  16. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC