c ++如何从路径字符串中删除文件名 [英] c++ how to remove filename from path string

查看:777
本文介绍了c ++如何从路径字符串中删除文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

const char *pathname = "..\somepath\somemorepath\somefile.ext";

如何将其转换为

"..\somepath\somemorepath"

p>

推荐答案

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

这篇关于c ++如何从路径字符串中删除文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆