linux

string path="home/yzbx/bin/run.sh";
cout<<path<<endl;
int pos=path.rfind('/');        //rfind 反向查找
cout<<pos<<endl;
string subpath=path.substr(0,pos);
cout<<subpath<<endl;
string filename=path.substr(pos+1,-1);
cout<<filename<<endl;

输出

home/yzbx/bin/run.sh
13
home/yzbx/bin
run.sh

windows

string path="home\\yzbx\\bin\\run.sh";
cout<<path<<endl;
int pos=path.rfind('\\');
cout<<pos<<endl;
string subpath=path.substr(0,pos);
cout<<subpath<<endl;
string filename=path.substr(pos+1,-1);
cout<<filename<<endl;

输出

home\yzbx\bin\run.sh
13
home\yzbx\bin
run.sh

Logo

更多推荐