2011-04-05 139 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);