How can I capture the path of the executable in C and put it inside a string ?
How can I capture the path of the executable in C and put it inside a string ?
If you want the path and not the name only, it does not have a universal default shape. You will have to consult the operating system and each has its own way. As I was not informed, I will indicate the 2 main ones:
Linux:
readlink("/proc/self/exe", buf, bufsize)
Windows
GetModuleFileName(NULL, buf, bufsize)
The argv[0]
provides only ./nome
, most of the time, can even provide the path if it is called from other specific places, but can not be trusted to be called from the appropriate location to provide the expected response. Also the code where this information is needed may not have access to this variable (it is out of main()
and has not been passed in any way). So it only works in very specific situations.
The main () function, where the program starts to run, has the following prototype:
int main (int argc, char *argv[])
The string argv [0] contains the name of the executable, the other strings of the array have the other parameters passed in the command line. The value of argc corresponds to the length of argv and usually should not be less than 1, since argv [0] always exists.