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

Categories

I have

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

how to transform that into

"..somepathsomemorepath"

?

See Question&Answers more detail:os

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...