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