c++从文件路径获取目录


场景

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM