views:

81

answers:

5

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?

A: 

Use GetModuleFileName http://msdn.microsoft.com/en-us/library/ms683197%28VS.85%29.aspx with the handle argument = 0

Victor Hurdugaci
Only if he only wants to compile on Windows
tloach
+3  A: 

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".

perimosocordiae
Er, you mean like basename() in the standard C library? ;)
SimonJ
Ah, right. I forgot that libgen.h was standard.
perimosocordiae
It's not standard C. It is in POSIX.
Steve Jessop
A: 

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

jitter
+2  A: 

Just search for the last /.

const char *exename = strrchr(argv[0], '/');
if (exename)
    // skip past the last /
    ++exename;   
else
    exename = argv[0];
R Samuel Klatchko
A: 

If it's available on your platform, there's a function char *basename(char *path). See basename documentation.

LnxPrgr3