I am retrieving the environment variables in win32 using GetEnvironmentStrings()
. It returns a char*
.
I want to search this string(char pointer) for a specific environmental variable (yes I know I can use GetEnvironmentVariable()
but I am doing it this way because I also want to print all the environment variables on the console aswell - I am just fiddling around).
So I thought I would convert the char*
to an std::string & use find on it (I know I can also use a c_string find function but I am more concerned about trying to copy a char*
into a std::string
). But the following code seems to not copy all of the char*
into the std::string
(it makes me think there is a \0
character in the char*
but its not actually the end).
char* a = GetEnvironmentStrings();
string b = string(a, sizeof(a));
printf( "%s", b.c_str() ); // prints =::=
Is there a way to copy a char*
into a std::string
(I know I can use strcpy()
to copy a const char*
into a string but not a char*
).
getenv
/GetEnvironmentVariable
function used is different but the result is the same... All the OP's got to do is replace thegetenv
call with aGetEnvironmentVariable
call. – Aiguillettegetenv
withGetEnvironmentStrings()
. – PresentationGetEnvironmentVariable
, it does actually have a different interface togetenv
and I wouldn't call it a duplicate anyway. – Presentation