convert a char* to std::string

后端 未结 11 1000
萌比男神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:48

    Most answers talks about constructing std::string.

    If already constructed, just use assignment operator.

    std::string oString;
    char* pStr;
    
    ... // Here allocate and get character string (e.g. using fgets as you mentioned)
    
    oString = pStr; // This is it! It copies contents from pStr to oString
    

提交回复
热议问题