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?
std::string has a constructor for this:
const char *s = "Hello, World!"; std::string str(s);
Just make sure that your
char * isn't
NULL, or else the behavior is undefined.
If you already know size of the char*, use this instead
char* data = ...; int size = ...; std::string myString(data, size);
This doesn't use strlen.
EDIT: If string variable already exists, use assign():
std::string myString; char* data = ...; int size = ...; myString.assign(data, size);