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

后端 未结 23 1650
温柔的废话
温柔的废话 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:10

    You can not use argv[0] for that purpose, usually it does contain full path to the executable, but not nessesarily - process could be created with arbitrary value in the field.

    Also mind you, the current directory and the directory with the executable are two different things, so getcwd() won't help you either.

    On Windows use GetModuleFileName(), on Linux read /dev/proc/procID/.. files.

提交回复
热议问题