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

来源:互联网 发布:微信帮砍价软件 编辑:程序博客网 时间:2024/05/17 00:50

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 在此某人载过跟头

原创粉丝点击