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


 

#include<iostream>
#include<string>
using namespace std;

int main()
{
    //string filePath = "D:\\statistics_chengqichao\\qcc\\biaoxiandianyun.xyz";
    //int nPos = filePath.rfind('\\');
    //string prefixPath = filePath.substr(0, filePath.length() - nPos-1);
    //cout << prefixPath << endl;





    string filename = "C:\\MyDirectory\\MyFile.txt";
    

    string directory;
    const size_t last_slash_idx = filename.rfind('\\');
    if (std::string::npos != last_slash_idx)
    {
        directory = filename.substr(0, last_slash_idx);

    }
    cout << directory << endl;
    system("pause");
    return 0;
}

 


免责声明!

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



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