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()
into an std::string
to store in an array. How can this be done?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Not sure why no one besides Erik mentioned this, but according to this page, the assignment operator works just fine. No need to use a constructor, .assign(), or .append().
Pass it in through the constructor:
You can use the function string.c_str() to go the other way:
I've just been struggling with MSVC2005 to use the
std::string(char*)
constructor just like the top-rated answer. As I see this variant listed as #4 on always-trusted http://en.cppreference.com/w/cpp/string/basic_string/basic_string , I figure even an old compiler offers this.It has taken me so long to realize that this constructor absolute refuses to match with
(unsigned char*)
as an argument ! I got these incomprehensible error messages about failure to match withstd::string
argument type, which was definitely not what I was aiming for. Just casting the argument withstd::string((char*)ucharPtr)
solved my problem... duh !