場景
c++從文件路徑獲取目錄
實現代碼
初始化是不正確的,因為需要轉義反斜杠:
string filename = "C:\\MyDirectory\\MyFile.bat";
如果存在,則提取目錄:
string directory;
const size_t last_slash_idx = filename.rfind('\\');
if (std::string::npos != last_slash_idx)
{
directory = filename.substr(0, last_slash_idx);
}
