When the compiler compiles code it spits out an executable. What I want to do is grab the name of that executable when it runs. The only problem is every where I search, I only get ways to grab the whole path of the executable.
If I have an executable named app.exe
, I want to output it in console.
People online say to use GetModuleFileName
, GetModuleBaseName
, and argv[0]
, but all of those give me the full path to the .exe, like C:\Users\Lone-PC\Desktop\app.exe
, and not just app.exe
alone.
I don't want to use any libraries and this is all packed in a console application for a windows machine.
I found what I want thanks to user HTNW.
I have to process the directory, from the mentioned methods like argv[0]
.
So I looked it up and borrowed code and made a function that will return the file name in a string.
Here is the code that solved my issue
string getFileName(string path) {
string filename = path;
const size_t last_slash_idx = filename.find_last_of("\\/");
if (std::string::npos != last_slash_idx) {
filename.erase(0, last_slash_idx + 1);
}
const size_t period_idx = filename.rfind('.');
return filename;
}