convert a char* to std::string

后端 未结 11 1024
萌比男神i
萌比男神i 2020-12-02 04:08

I need to use an std::string to store data retrieved by fgets(). To do this I need to convert the char* return value from fgets(

11条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 04:39

    Pass it in through the constructor:

    const char* dat = "my string!";
    std::string my_string( dat );
    

    You can use the function string.c_str() to go the other way:

    std::string my_string("testing!");
    const char* dat = my_string.c_str();
    

提交回复
热议问题