How do I get the directory that a program is running from?

后端 未结 23 1651
温柔的废话
温柔的废话 2020-11-22 04:39

Is there a platform-agnostic and filesystem-agnostic method to obtain the full path of the directory from where a program is running using C/C++? Not to be confused with the

23条回答
  •  情深已故
    2020-11-22 05:17

    On Windows the simplest way is to use the _get_pgmptr function in stdlib.h to get a pointer to a string which represents the absolute path to the executable, including the executables name.

    char* path;
    _get_pgmptr(&path);
    printf(path); // Example output: C:/Projects/Hello/World.exe
    

提交回复
热议问题