Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

c++ how to remove filename from path string

I have

const char *pathname = "..somepathsomemorepathsomefile.ext";

how to transform that into

"..somepathsomemorepath"

?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The easiest way is to use find_last_of member function of std::string

string s1("../somepath/somemorepath/somefile.ext");
string s2("..\somepath\somemorepath\somefile.ext");
cout << s1.substr(0, s1.find_last_of("\/")) << endl;
cout << s2.substr(0, s2.find_last_of("\/")) << endl;

This solution works with both forward and back slashes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...