getting cout output to a std::string
Asked Answered
I

4

52

I have the following cout statement. I use char arrays because I have to pass to vsnprintf to convert variable argument list and store in Msg.

Is there any way we can get cout output to C++ std::string?

char Msg[100];
char appname1[100];
char appname2[100];
char appname3[100]; 

// I have some logic in function which some string is assigned to Msg.
std::cout << Msg << " "<< appname1 <<":"<< appname2 << ":" << appname3 << " " << "!" << getpid() <<" " << "~" << pthread_self() << endl;
Incredible answered 4/3, 2011 at 11:34 Comment(0)
A
90

You can replace cout by a stringstream.

std::stringstream buffer;
buffer << "Text" << std::endl;

You can access the string using buffer.str().

To use stringstream you need to use the following libraries:

#include <string>  
#include <iostream> 
#include <sstream>   
Abnormity answered 4/3, 2011 at 11:37 Comment(3)
@venkysmarty: You can clear the buffer after each string. If the string are separated with spaces or something like that, you can split it.Gabey
How can I use this with printf? clearly i cant replace printf with buffer unlike the cout!Thesda
@Hossein, indeed this doesn't work with printf, in that case you'll have to use snprintf.Gabey
B
5

You can use std::stringstream

http://www.cplusplus.com/reference/iostream/stringstream/

Becerra answered 4/3, 2011 at 11:39 Comment(3)
could you provide a quick description of your solution?Highpitched
The accepted answer is not actually the answer to the question, as this is not about capturing cout to a string: I hoped you could write about that!Highpitched
@campa May be you should describe you problem in a separate question? It's not clear what you are trying to do.Becerra
H
3

If you can change the code then use ostringstream (or stringstream) instead of cout.

If you cannot change the code and want to "capture" what is being output you can redirect your output or pipe it.

It may then be possible for your process to read the file or get the piped information through shared memory.

Henton answered 4/3, 2011 at 12:16 Comment(1)
Could you describe how to capture cout.. ?Highpitched
D
0
#include <stdio.h>

#include <iostream>
#include <string>
#include <sstream>

// This way we won't have to say std::ostringstream or std::cout or std::string...
using namespace std;

/** Simulates system specific method getpid()... */
int faux_getpid(){
    return 1234;
}

/** Simulates system specific method pthread_self()... */
int faux_pthread_self(){
    return 1111;
}

int main(int argc, char** argv){

    // Create a char[] array of 100 characters...
    // this is the old-fashioned "C" way of storing a "string"
    // of characters..
    char Msg[100];


    // Try using C++-style std::string rather than char[],
    // which can be overrun, leading to 
    // a segmentation fault.
    string s_appname1; 

    // Create old-fashioned char[] array of 100 characters...
    char appname2[100];

    // Create old-fashioned char[] array of 100 characters...
    char appname3[100]; 

    // Old-fashioned "C" way of copying "Hello" into Msg[] char buffer...
    strcpy(Msg, "Hello");

    // C++ way of setting std::string s_appname equal to "Moe"...
    s_appname1 = "Moe";

    // Old-fashioned "C" way of copying "Larry" into appname2[] char buffer...
    strcpy(appname2, "Larry");

    // Old-fashioned "C" way of copying "Shemp" into appname3[] char buffer...
    strcpy(appname3, "Shemp");

    // Declare le_msg to be a std::ostringstream...
    // this allows you to use the C++ "put-to" operator <<
    // but it will "put-to" the string-stream rather than
    // to the terminal or to a file...
    ostringstream le_msg;

    // Use put-to operator << to "write" Msg, s_appname1, s_appname2, etc...
    // to the ostringstream...not to the terminal...
    le_msg << Msg << " "<< s_appname1 <<":"<< appname2 << ":" << appname3 << " " << "!" << faux_getpid() <<" " << "~" << faux_pthread_self();

    // Print the contents of le_msg to the terminal -- std::cout --
    // using the put-to operator << and using le_msg.str(),
    // which returns a std::string.
    cout << "ONE: le_msg = \"" << le_msg.str() << "\"..." << endl;

    // Change contents of appname3 char[] buffer to "Curly"...
    strcpy(appname3, "Curly");

    // Clear the contents of std::ostringstream le_msg
    // -- by setting it equal to "" -- so you can re-use it.
    le_msg.str(""); 

    // Use put-to operator << to "write" Msg, s_appname1, s_appname2, etc...
    // to the newly cleared ostringstream...not to the terminal...
    // but this time appname3 has been set equal to "Curly"...
    le_msg << Msg << " "<< s_appname1 <<":"<< appname2 << ":" << appname3 << " " << "!" << faux_getpid() <<" " << "~" << faux_pthread_self();

    // Print the new contents of le_msg to the terminal using the  
    // put-to operator << and using le_msg.str(),
    // which returns a std::string.
    cout << "TWO: le_msg = \"" << le_msg.str() << "\"..." << endl;

    // This time, rather than using put-to operator << to "write"
    // to std::ostringstream le_msg, we'll explicitly set it equal
    // to "That's all Folks!"
    le_msg.str("That's all Folks!");

    // Print the new contents of le_msg "That's all Folks!" to  
    // the terminal via le_msg.str()
    cout << "THREE: le_msg = \"" << le_msg.str() << "\"..." << endl;

    // Exit main() with system exit value of zero (0), indicating
    // success...
    return 0;

}/* main() */

OUTPUT:

ONE: le_msg = "Hello Moe:Larry:Shemp !1234 ~1111"...
TWO: le_msg = "Hello Moe:Larry:Curly !1234 ~1111"...
THREE: le_msg = "That's all, folks!"...
Dual answered 21/7, 2016 at 15:30 Comment(2)
can you explain what you did in the answerMaggot
I added comments to the code above, @depperm, hopefully it makes a bit more sense now. See also the official documentation of std::ostringstream.Dual

© 2022 - 2024 — McMap. All rights reserved.