将 cout 输出输出到 std::string

发布于 2024-10-20 10:30:24 字数 576 浏览 1 评论 0原文

我有以下 cout 语句。我使用 char 数组,因为我必须传递给 vsnprintf 来转换变量参数列表并存储在 Msg 中。

有什么方法可以将 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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清醇 2024-10-27 10:30:24

您可以将 cout 替换为 stringstream

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

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

要使用stringstream,您需要使用以下库:

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

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>   
信仰 2024-10-27 10:30:24

如果您可以更改代码,请使用 ostringstream(或 stringstream)而不是 cout。

如果您无法更改代码并想要“捕获”正在输出的内容,您可以重定向输出或通过管道传输它。

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

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.

心凉 2024-10-27 10:30:24
#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() */

输出:

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!"...
#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!"...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文