Hi I'm trying to use the name of the executable and an usage string, I'm using argv[0]
for such purpose but instead of the name of the executable itself it gives me the complete path to it.
Is there any way to get only the executable name?
Hi I'm trying to use the name of the executable and an usage string, I'm using argv[0]
for such purpose but instead of the name of the executable itself it gives me the complete path to it.
Is there any way to get only the executable name?
Use GetModuleFileName http://msdn.microsoft.com/en-us/library/ms683197%28VS.85%29.aspx with the handle argument = 0
As far as I know, (on linux, at least) you just have to extract the executable name from the char* yourself.
The easiest way to do that is to use basename(argv[0])
, which you can get by including "libgen.h".
Just use the last part of the path-string. Some combination of a call to strrchr
(get last path delimiter) and e.g. strcpy
or similar to copy out the part from last path delimiter to end
Just search for the last /.
const char *exename = strrchr(argv[0], '/');
if (exename)
// skip past the last /
++exename;
else
exename = argv[0];
If it's available on your platform, there's a function char *basename(char *path)
. See basename documentation.