开发者

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?


Just search for the last /.

const char *exename = strrchr(argv[0], '/');
if (exename)
    // skip past the last /
    ++exename;   
else
    exename = argv[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".


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


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜