简体   繁体   English

有没有一种简单的方法可以从c ++中的路径获取文件名?

[英]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' ;
}

Demo here 在这里演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM