linux 查找执行程序的当前路径

linux shell pwd 显示当前路径

假若有test.cpp

g++ test.cpp -o test

./test

想在test中找到当前执行程序所在的路径

可以再test.cpp中使用readlink函数

具体见如下实例:

#include<iostream>#include<unistd.h>#include<dirent.h>#include<string.h>#include<string>using namespace std;int main(){char buff[1024];memset(buff,0,sizeof(buff));int n = readlink(“/proc/self/exe”,buff,1023);if(n<0){cout<<“Get Path failed”<<endl;return -1;}string path = buff;int nLen = path.rfind(‘/’);path.erase(nLen,path.length() – nLen);cout<<“zui zhong lu jing :”<<path<<endl;return 0;}

谨记是/proc/self/exe 在此某人载过跟头

莫找借口失败,只找理由成功。(不为失败找理由,要为成功找方法)

linux 查找执行程序的当前路径

相关文章:

你感兴趣的文章:

标签云: