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

后端 未结 23 1682
温柔的废话
温柔的废话 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 04:59

    Filesystem TS is now a standard ( and supported by gcc 5.3+ and clang 3.9+ ), so you can use current_path() function from it:

    std::string path = std::experimental::filesystem::current_path();
    

    In gcc (5.3+) to include Filesystem you need to use:

    #include 
    

    and link your code with -lstdc++fs flag.

    If you want to use Filesystem with Microsoft Visual Studio, then read this.

提交回复
热议问题