【问题标题】:C/C++ - executable pathC/C++ - 可执行路径
【发布时间】:2012-01-24 15:07:19
【问题描述】:

我想得到当前可执行文件的文件路径,最后没有可执行文件名。

我正在使用:

char path[1024];
uint32_t size = sizeof(path);
if (_NSGetExecutablePath(path, &size) == 0)
    printf("executable path is %s\n", path);
else
    printf("buffer too small; need size %u\n", size);

它可以工作,但这会在末尾添加可执行文件名称。

【问题讨论】:

    标签: c++ c path


    【解决方案1】:

    使用dirname

    【讨论】:

      【解决方案2】:
      char* program_name = dirname(path);
      

      【讨论】:

        【解决方案3】:

        dirname(path); 获取路径后应该返回没有可执行文件的路径,即在 Unix 系统上,对于 Windows,你可以做一些 strcpy/strcat 魔术。

        对于目录名,您需要包含 #include <libgen.h>...

        【讨论】:

        • OP 正在调用 Mac OS X 函数,因此他可以访问 Unix 例程。
        • 如果您不介意引入 SHLWAPI,可以在 Windows 上使用 PathRemoveFileSpec。或_splitpath_s
        • 是的,我忘记了_splitpath xD,至于@chrisaycock,抱歉我不知道,idk mac API
        【解决方案4】:

        对于 Linux:
        执行系统命令的函数

        int syscommand(string aCommand, string & result) {
            FILE * f;
            if ( !(f = popen( aCommand.c_str(), "r" )) ) {
                    cout << "Can not open file" << endl;
                    return NEGATIVE_ANSWER;
                }
                const int BUFSIZE = 4096;
                char buf[ BUFSIZE ];
                if (fgets(buf,BUFSIZE,f)!=NULL) {
                    result = buf;
                }
                pclose( f );
                return POSITIVE_ANSWER;
            }
        

        然后我们得到应用名称

        string getBundleName () {
            pid_t procpid = getpid();
            stringstream toCom;
            toCom << "cat /proc/" << procpid << "/comm";
            string fRes="";
            syscommand(toCom.str(),fRes);
            size_t last_pos = fRes.find_last_not_of(" \n\r\t") + 1;
            if (last_pos != string::npos) {
                fRes.erase(last_pos);
            }
            return fRes;
        }
        

        然后我们提取应用路径

            string getBundlePath () {
            pid_t procpid = getpid();
            string appName = getBundleName();
            stringstream command;
            command <<  "readlink /proc/" << procpid << "/exe | sed \"s/\\(\\/" << appName << "\\)$//\"";
            string fRes;
            syscommand(command.str(),fRes);
            return fRes;
            }
        

        不要忘记在后面修剪线

        【讨论】:

          【解决方案5】:

          Ubuntu 的最佳解决方案!

          std::string getpath() {
            char buf[PATH_MAX + 1];
            if (readlink("/proc/self/exe", buf, sizeof(buf) - 1) == -1)
            throw std::string("readlink() failed");
            std::string str(buf);
            return str.substr(0, str.rfind('/'));
          }
          
          int main() {
            std::cout << "This program resides in " << getpath() << std::endl;
            return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 2012-07-21
            • 2019-09-28
            • 1970-01-01
            • 2011-10-23
            • 1970-01-01
            • 2018-10-17
            • 2017-05-25
            • 2010-12-12
            • 1970-01-01
            相关资源
            最近更新 更多