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

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

    Boost Filesystem's initial_path() behaves like POSIX's getcwd(), and neither does what you want by itself, but appending argv[0] to either of them should do it.

    You may note that the result is not always pretty--you may get things like /foo/bar/../../baz/a.out or /foo/bar//baz/a.out, but I believe that it always results in a valid path which names the executable (note that consecutive slashes in a path are collapsed to one).

    I previously wrote a solution using envp (the third argument to main() which worked on Linux but didn't seem workable on Windows, so I'm essentially recommending the same solution as someone else did previously, but with the additional explanation of why it is actually correct even if the results are not pretty.

    0 讨论(0)
  • 2020-11-22 05:05

    The linux bash command which progname will report a path to program.

    Even if one could issue the which command from within your program and direct the output to a tmp file and the program subsequently reads that tmp file, it will not tell you if that program is the one executing. It only tells you where a program having that name is located.

    What is required is to obtain your process id number, and to parse out the path to the name

    In my program I want to know if the program was executed from the user's bin directory or from another in the path or from /usr/bin. /usr/bin would contain the supported version. My feeling is that in Linux there is the one solution that is portable.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 05:11
    #include <windows.h>
    using namespace std;
    
    // The directory path returned by native GetCurrentDirectory() no end backslash
    string getCurrentDirectoryOnWindows()
    {
        const unsigned long maxDir = 260;
        char currentDir[maxDir];
        GetCurrentDirectory(maxDir, currentDir);
        return string(currentDir);
    }
    
    0 讨论(0)
  • 2020-11-22 05:12

    This is from the cplusplus forum

    On windows:

    #include <string>
    #include <windows.h>
    
    std::string getexepath()
    {
      char result[ MAX_PATH ];
      return std::string( result, GetModuleFileName( NULL, result, MAX_PATH ) );
    }
    

    On Linux:

    #include <string>
    #include <limits.h>
    #include <unistd.h>
    
    std::string getexepath()
    {
      char result[ PATH_MAX ];
      ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX );
      return std::string( result, (count > 0) ? count : 0 );
    }
    

    On HP-UX:

    #include <string>
    #include <limits.h>
    #define _PSTAT64
    #include <sys/pstat.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    std::string getexepath()
    {
      char result[ PATH_MAX ];
      struct pst_status ps;
    
      if (pstat_getproc( &ps, sizeof( ps ), 0, getpid() ) < 0)
        return std::string();
    
      if (pstat_getpathname( result, PATH_MAX, &ps.pst_fid_text ) < 0)
        return std::string();
    
      return std::string( result );
    }
    
    0 讨论(0)
提交回复
热议问题