how to convert char string into a std string
I have following code and I cant figure out how to convert char stri开发者_Python百科ng into a std string. Can someone help me out here?
char Path[STRING_MAX];
test->getValue(Config::CODE,Path);
size_t found;
cout << "Splitting: " << Path << endl;
found=Path.find_last_of("/");
cout << " folder: " << Path.substr(0,found) << endl;
cout << " file: " << Path.substr(found+1) << endl;
Use this:
std::string sPath(Path);
or:
std::string sPath = Path;
or:
std::string sPath;
sPath.assign(Path);
If the C-string is null-terminated, then the approaches Erik demonstrated will work fine.
However if the C-string is not null-terminated, then one must do the following (assuming one knows the length of the valid data):
std::string sPath(Path, PathLength);
or:
// given std::string sPath
sPath.assign(Path, PathLength);
std::string pathstr(Path);
will convert it using the constructor for std::string
.
精彩评论