Gerry K.
Gerry K.

Reputation: 3

Is it possible to have the name of the executable without the path?

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?

Upvotes: 0

Views: 1173

Answers (6)

ClosedID
ClosedID

Reputation: 637

You could use getprogname() if the name of the program is set by your OS.

Upvotes: 0

Left For Archive
Left For Archive

Reputation: 2666

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

Upvotes: 1

perimosocordiae
perimosocordiae

Reputation: 17797

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

Upvotes: 4

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76531

Just search for the last /.

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

Upvotes: 5

jitter
jitter

Reputation: 54605

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

Upvotes: 0

Victor Hurdugaci
Victor Hurdugaci

Reputation: 28425

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

Upvotes: 0

Related Questions