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

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

    Use realpath() in stdlib.h like this:

    char *working_dir_path = realpath(".", NULL);
    
    0 讨论(0)
  • 2020-11-22 04:55

    Just to belatedly pile on here,...

    there is no standard solution, because the languages are agnostic of underlying file systems, so as others have said, the concept of a directory based file system is outside the scope of the c / c++ languages.

    on top of that, you want not the current working directory, but the directory the program is running in, which must take into account how the program got to where it is - ie was it spawned as a new process via a fork, etc. To get the directory a program is running in, as the solutions have demonstrated, requires that you get that information from the process control structures of the operating system in question, which is the only authority on this question. Thus, by definition, its an OS specific solution.

    0 讨论(0)
  • 2020-11-22 04:55

    As Minok mentioned, there is no such functionality specified ini C standard or C++ standard. This is considered to be purely OS-specific feature and it is specified in POSIX standard, for example.

    Thorsten79 has given good suggestion, it is Boost.Filesystem library. However, it may be inconvenient in case you don't want to have any link-time dependencies in binary form for your program.

    A good alternative I would recommend is collection of 100% headers-only STLSoft C++ Libraries Matthew Wilson (author of must-read books about C++). There is portable facade PlatformSTL gives access to system-specific API: WinSTL for Windows and UnixSTL on Unix, so it is portable solution. All the system-specific elements are specified with use of traits and policies, so it is extensible framework. There is filesystem library provided, of course.

    0 讨论(0)
  • 2020-11-22 04:58

    If you fetch the current directory when your program first starts, then you effectively have the directory your program was started from. Store the value in a variable and refer to it later in your program. This is distinct from the directory that holds the current executable program file. It isn't necessarily the same directory; if someone runs the program from a command prompt, then the program is being run from the command prompt's current working directory even though the program file lives elsewhere.

    getcwd is a POSIX function and supported out of the box by all POSIX compliant platforms. You would not have to do anything special (apart from incliding the right headers unistd.h on Unix and direct.h on windows).

    Since you are creating a C program it will link with the default c run time library which is linked to by ALL processes in the system (specially crafted exceptions avoided) and it will include this function by default. The CRT is never considered an external library because that provides the basic standard compliant interface to the OS.

    On windows getcwd function has been deprecated in favour of _getcwd. I think you could use it in this fashion.

    #include <stdio.h>  /* defines FILENAME_MAX */
    #ifdef WINDOWS
        #include <direct.h>
        #define GetCurrentDir _getcwd
    #else
        #include <unistd.h>
        #define GetCurrentDir getcwd
     #endif
    
     char cCurrentPath[FILENAME_MAX];
    
     if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
         {
         return errno;
         }
    
    cCurrentPath[sizeof(cCurrentPath) - 1] = '\0'; /* not really required */
    
    printf ("The current working directory is %s", cCurrentPath);
    
    0 讨论(0)
  • 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 <experimental/filesystem>
    

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

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

    0 讨论(0)
  • 2020-11-22 04:59

    Maybe concatenate the current working directory with argv[0]? I'm not sure if that would work in Windows but it works in linux.

    For example:

    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    
    int main(int argc, char **argv) {
        char the_path[256];
    
        getcwd(the_path, 255);
        strcat(the_path, "/");
        strcat(the_path, argv[0]);
    
        printf("%s\n", the_path);
    
        return 0;
    }
    

    When run, it outputs:

    jeremy@jeremy-desktop:~/Desktop$ ./test
    /home/jeremy/Desktop/./test

    0 讨论(0)
提交回复
热议问题