我有一個函數可以查找字符串中目錄的最後一位。例如: 「C:\ Lolcats \ pie \ ambulance \」應該返回「救護車」。然而,它會返回一些從未見過的奇怪字符,如男性箭頭符號和其他一些奇怪的東西。如何返回目錄的末尾
string App::getlastName(string cakes){
//finds the name of the last folder in a directory
string name;
string temp;//popback all of temp into name to invert it
cakes.pop_back();
char i = cakes[cakes.length()-1];
while (i != '\\'){
temp.push_back(cakes[i]);
cakes.pop_back();
i = cakes[cakes.length()-1];
} //-1?
for (int j = 0; j<temp.length(); ++j){
name.push_back(temp.back());
temp.pop_back();
}
return name;
}
這大概是我曾經寫過的最糟糕的功能之一,但我想不出怎麼回事纏鬥到底關閉:(有人可以幫我嗎?d
注該功能犯規需要找到一個文件的名稱,它會僅僅是文件夾
爲什麼不使用'basename',無論是從標準C庫還是從boost :: filesystem? –
或友好的正則表達式?我的意思是,即使是一連串的substr調用都會比這更好。 –
'temp.push_back(cakes [i]);'是你的大錯。我是你的字符串中的一個字符,而不是你的字符串的索引。 –