Copying NOT null terminated C-string to std::string
Hi *!
I get from a lib a not null-terminated string back. I am want to copy it to my local std::string. I also get back the total length of C-string. Here is what i am doing
PHP Code:
char* pChr = NULL;
short nChrLen = 0;
std::string szMyString = "";
// Call some lib functions with pChr and &nChrLen....
if(pChr){
szMyString = pChr;
szMyString[nChrLen] = '\\0';
}
It works correctly but is it the corrrect way, coz if i don't null terminate the string i get some junk stuff (thats obvious) but the copying of string reads some part of memory that it is not supposed to. Whats the correct way to do that????????
Thanks in Advance,
Regards,
Usman.
Re: Copying NOT null terminated C-string to std::string
Quote:
Originally posted by usman999_1
Hi *!
I get from a lib a not null-terminated string back. I am want to copy it to my local std::string.
Use the std::string::assign() member function:
Code:
szMyString.assign(pChr, nChrLen);
Regards,
Paul McKenzie