开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜