使用GetOpenFileName只获取路径之外的文件名
Getting only the filename out of the path with GetOpenFileName
所以我有这个代码:
OPENFILENAME ofn;
char file_name[100];
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = NULL;
ofn.lpstrFile = file_name;
ofn.lpstrFile[0] = ' ';
ofn.nMaxFile = 100;
ofn.lpstrFilter = "Dynamic Link Libraries (.dll) *.dll";
ofn.nFilterIndex = 1;
GetOpenFileName(&ofn);
cout << (const char*)ofn.lpstrFile << endl;
它只是定义窗口的属性,然后用GetOpenFileName(&ofn)
打开一个文件,但当我打印lpstrFile
时,我会得到我选择的文件的完整路径。
现在我的问题是,在c++上,如何使用文本替换函数或内置窗口函数,从ofn.lpstrFile
中只获得文件名exfile.dll
,而不获得C:/hello/file.dll
。
提前谢谢。
使用std::filesystem::path
类获得:
std::filesystem::path myFile = ofn.lpstrFile;
std::filesystem::path fullname = myFile.filename();
cout << fullname.c_str() << endl;
它也适用于@WhozCraig:指出的方法
#pragma comment(lib, "shlwapi.lib")
#include <Shlwapi.h>
PathStripPath(ofn.lpstrFile);
相关文章:
- 有没有一种简单的方法可以从 c++ 中的路径获取文件名?
- 从 C++ 中的文件路径中提取文件名
- 指定用户定义的文件名的路径
- 从 QFileSystemModel 中的文件路径和文件名获取 QModelIndex
- 如何更改变量文件名的文件路径?
- Xcode 8.3.2 中的错误警告:文件的非可移植路径 - 指定的路径与磁盘上的文件名不同
- #line 指令的 nul.h 文件名的文件路径不正确
- 从文件名中提取文件名,路径来自参数
- 运行正则表达式时未选中的异常 - 从文件路径获取不带扩展名的文件名
- Simple_ls.cpp的C++Boost编译错误-未找到对Boost的引用::文件系统::路径::文件名
- 获取模块文件名不返回路径
- 向文件名参数(LPCSTR)添加预先声明的路径
- 如何生成唯一的文件名或路径
- c++将文件的路径组合为字符串和文件名
- 在vc++中,只从文件的完整路径中查找文件名
- 在 mingw 下编译时出现路径查找文件名错误
- 从流中获取文件名(或路径)
- 如何连接路径和不同的文件名 - C++
- C ++如何从路径字符串中删除文件名
- 使文件名保留在boost中的路径中