[英]Is there a simple way to get the filename from a path in c++?
Currently I am getting a file's path with GetModuleFileName
and storing it in szExeFilePath by doing: 目前,我正在通过
GetModuleFileName
获取文件的路径,并通过执行以下操作将其存储在szExeFilePath中:
TCHAR szExeFilePath[MAX_PATH];
GetModuleFileName(NULL, szExeFilePath, MAX_PATH);
And that returns C:\\\\dev\\\\program\\\\Debug\\\\program.exe
然后返回
C:\\\\dev\\\\program\\\\Debug\\\\program.exe
However I also want to store just the program.exe
. 但是我也只想存储
program.exe
。 I looked around and saw _splitpath_s
might be the easiest way of doing this. 我环顾四周,发现
_splitpath_s
可能是最简单的方法。 The only problem is that I didn't see any explanation on how to actually use _splitpath_s
and I can't get it to work at all. 唯一的问题是,我没有看到有关如何实际使用
_splitpath_s
任何解释,而且我根本无法使它正常工作。
So basically I am asking how to use _splitpath_s
or if there is a simpler/easier method of getting the filename of the executable. 所以基本上我在问如何使用
_splitpath_s
或是否有一种更简单/更容易的方法来获取可执行文件的文件名。
However I also want to store just the program.exe
但是我也只想存储program.exe
With C++17 you could simple use std::filesystem
使用C ++ 17,您可以简单地使用
std::filesystem
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
std::cout << fs::path("C:/dev/program/Debug/program.exe").filename() << '\n' ;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.