문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top