usustarr
usustarr

Reputation: 418

how to convert char string into a std string

I have following code and I cant figure out how to convert char string 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;

Upvotes: 1

Views: 592

Answers (3)

ildjarn
ildjarn

Reputation: 62995

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);

Upvotes: 3

jonsca
jonsca

Reputation: 10381

std::string pathstr(Path); will convert it using the constructor for std::string.

Upvotes: 1

Erik
Erik

Reputation: 91320

Use this:

std::string sPath(Path);

or:

std::string sPath = Path;

or:

std::string sPath;
sPath.assign(Path);

Upvotes: 4

Related Questions