且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c ++如何从路径字符串中删除文件名

更新时间:2023-02-23 20:10:40

最简单的方法是使用 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.