C++ 为什么我在C+中得到了错误的当前目录+;linux的应用

C++ 为什么我在C+中得到了错误的当前目录+;linux的应用,c++,c,netbeans,C++,C,Netbeans,我使用NETBeas程序C++,我想得到可执行文件的当前绝对路径 (~/NetBeansWorkSpace/project_1/dist/Debug/GNU-Linux-x86/executableFileName) 所以我用 1、系统(“pwd”) 2,getcwd(缓冲区,缓冲区大小) 然后单击“运行”按钮,但它们都得到了错误的路径:~/NetBeansWorkSpace/project\u 1 令人惊讶的是,我主持了这次聚会 cd~/NetBeansWorkSpace/project_1/

我使用NETBeas程序C++,我想得到可执行文件

的当前绝对路径
(~/NetBeansWorkSpace/project_1/dist/Debug/GNU-Linux-x86/executableFileName)

所以我用

1、
系统(“pwd”)

2,
getcwd(缓冲区,缓冲区大小)

然后单击“运行”按钮,但它们都得到了错误的路径:~/NetBeansWorkSpace/project\u 1

令人惊讶的是,我主持了这次聚会

cd~/NetBeansWorkSpace/project_1/dist/Debug/GNU-Linux-x86/executableFileName

/executableFileName

我找到了正确的道路


这就是为什么???

NetBeans通过在路径
dist/Debug/GNU-Linux-x86/
前面加前缀,从
~/NetBeansWorkSpace/project_1/
启动应用程序


打开一个shell,做一张cd
~/NetBeansWorkSpace/project\u 1/
,然后做
dist/Debug/GNU-Linux-x86/executableFileName
,你将得到与从NetBeans运行应用程序相同的结果。

没有问题-NetBeans正在运行你的程序,当前工作目录设置为项目目录(
~/NetBeansWorkSpace/project_1


您的程序不应依赖于当前目录与您的程序所在的目录相同。请查看是否要查看获取程序绝对路径的几种不同方法。

正如其他人所述,NetBeans正在运行应用程序之前设置工作目录。如果要获取对于可执行文件的目录,我相信下面应该可以工作

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char const* *argv) {
    char *resolved = realpath(argv[0], NULL);
    if (resolved != NULL) {
        char *fname = strrchr(resolved, '/');
        if (fname != NULL) {
            fname[1] = '\0';
        }
        printf("absolute path of %s is %s\n", argv[0], resolved);
        free(resolved);
    } else {
        perror("realpath");
    }
    return EXIT_SUCCESS;
}
#包括
#包括
#包括
int main(int argc,字符常量**argv){
char*resolved=realpath(argv[0],NULL);
如果(已解决!=NULL){
char*fname=strrchr(已解析,“/”);
如果(fname!=NULL){
fname[1]='\0';
}
printf(“%s的绝对路径是%s\n”,argv[0],已解析);
自由(已解决);
}否则{
perror(“realpath”);
}
返回退出成功;
}
适用于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;
    }
int syscommand(字符串aCommand、字符串和结果){
文件*f;
如果(!(f=popen(aCommand.c_str(),“r”)){

您的IDE在运行程序时似乎正在设置程序的当前目录。一般来说,您不能依赖工作目录来确定可执行文件所在的位置。
pwd
getcwd
返回当前工作目录,而不是指向可执行文件位置的路径。。。
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;
    }