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;
You can replace
cout
by astringstream
.You can access the string using
buffer.str()
.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.
OUTPUT:
You can use std::stringstream
http://www.cplusplus.com/reference/iostream/stringstream/