In your main function, argv[0]
is the name of the executable from the command line
#include <stdio.h>
int main(int argc, char ** argv)
{
printf("%s", argv[0]);
return 0;
}
Live Demo
This prints the command name, which is the directory relative to the current working directory, plus the executable name (if available, which is not guaranteed)
To get the current working directory, use getcwd()
standard library C function.
Extracting the file name from the command path in argv[0]
is platform specific : unix use slashes '/
', windows allows mixed uses of slash /
and backslash
, and any other platform could use any other path separator. Extracting the file name from a path requires a cross-platform library, such as Qt or Boost. On POSIX environments, basename could be used.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…