2011-04-05 138 views
1

我有以下代码,我无法弄清楚如何将char字符串转换为std字符串。有人能帮助我吗?如何将char字符串转换为std字符串

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; 
+0

请缩进代码4个空格让它正确显示。 – Swiss 2011-04-05 18:23:13

回答

3

使用此:

std::string sPath(Path); 

或:

std::string sPath = Path; 

或:

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

std::string pathstr(Path);将使用构造转换它std::string

+0

非常感谢你的每一个。它确实工作! – usustarr 2011-04-05 19:16:32

2

如果C字符串空终止,那么Erik演示的方法将工作正常。

然而,如果C-串不是空终止,则一个必须执行下列操作(假定人知道的有效数据的长度):

std::string sPath(Path, PathLength); 

或:

// given std::string sPath 
sPath.assign(Path, PathLength);