2011-03-04 85 views
20

我有以下cout声明。我使用char数组,因为我必须传递给vsnprintf来转换可变参数列表并将其存储在Msg中。获取cout输出到std :: string

有什么办法可以让我们得到cout输出到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; 

回答

44

您可以用stringstream替换cout

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

您可以使用buffer.str()访问字符串。

+0

我在COUT mutilple串,我怎么做。谢谢 – venkysmarty 2011-03-04 12:04:47

+1

@venkysmarty:你可以在每个字符串之后清除缓冲区。如果字符串用空格或类似的东西分隔,你可以拆分它。 – 2011-03-04 12:13:16

+0

如何在printf中使用它? 显然我不能用cout代替printf和buffer! – Breeze 2015-10-23 18:05:20

2

如果你可以改变代码,然后用ostringstream(或字符串流),而不是COUT。

如果您不能更改代码并想“捕获”输出内容,您可以重定向输出或管道输出。

然后,您的进程可能会读取文件或通过共享内存获取管道信息。

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!"... 
+1

你能解释一下你在答案中做了什么 – depperm 2016-07-21 15:40:14

+0

我在上面的代码中添加了评论,@depperm,希望它现在更有意义。另请参见[std :: ostringstream的官方文档](http://www.cplusplus.com/reference/sstream/ostringstream/)。 – 2016-07-28 15:43:25