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

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

    No, there's no standard way. I believe that the C/C++ standards don't even consider the existence of directories (or other file system organizations).

    On Windows the GetModuleFileName() will return the full path to the executable file of the current process when the hModule parameter is set to NULL. I can't help with Linux.

    Also you should clarify whether you want the current directory or the directory that the program image/executable resides. As it stands your question is a little ambiguous on this point.

提交回复
热议问题