Linux中获取当前程序路径的方法
2012-05-22 13:37
483 查看
1、命令行实现:转自:http://www.linuxdiyf.com/viewarticle.php?id=84177
#!/bin/sh
cur_dir=$(pwd)
echo $cur_dir
注意:在cur_dir后没空格,=后面也不能有空格,不然它会认为空格不是路径而报错
2、程序实现:转自:http://topic.csdn.net/u/20071217/13/78e81ffa-b30c-4685-a58a-2eb5e181b825.html
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int getpath(char *buf)
{
long size;
char *ptr;
size = pathconf(".",_PC_PATH_MAX);
if((ptr = (char*)malloc((size_t)size)) != NULL)
{
memset(ptr,0,size);
sprintf(ptr,"/proc/%d/exe",getpid());
}
else
return -1;
return readlink(ptr,buf,size);
}
int main()
{
char buf[128];
getpath(buf);
printf("%s\n",buf);
}
转自:http://hi.baidu.com/jrckkyy/blog/item/6f74ebee3b4768e3b3fb9542.html
http://hi.baidu.com/xlt1888/blog/item/0958fd86668b73cc9123d99f.html
#include <unistd.h>
#include <stdio.h>
int main(int argc , char* argv[])
{
char buf[1024] = { 0 };
int n=0;
n =readlink("/proc/self/exe" , buf , sizeof(buf));
if( n > 0 && n < (int)sizeof(buf))
{
Buf
= ‘\0’;
printf("%s\n", buf);
}
}
还可以利用getcwd函数来实现。
#!/bin/sh
cur_dir=$(pwd)
echo $cur_dir
注意:在cur_dir后没空格,=后面也不能有空格,不然它会认为空格不是路径而报错
2、程序实现:转自:http://topic.csdn.net/u/20071217/13/78e81ffa-b30c-4685-a58a-2eb5e181b825.html
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int getpath(char *buf)
{
long size;
char *ptr;
size = pathconf(".",_PC_PATH_MAX);
if((ptr = (char*)malloc((size_t)size)) != NULL)
{
memset(ptr,0,size);
sprintf(ptr,"/proc/%d/exe",getpid());
}
else
return -1;
return readlink(ptr,buf,size);
}
int main()
{
char buf[128];
getpath(buf);
printf("%s\n",buf);
}
转自:http://hi.baidu.com/jrckkyy/blog/item/6f74ebee3b4768e3b3fb9542.html
http://hi.baidu.com/xlt1888/blog/item/0958fd86668b73cc9123d99f.html
#include <unistd.h>
#include <stdio.h>
int main(int argc , char* argv[])
{
char buf[1024] = { 0 };
int n=0;
n =readlink("/proc/self/exe" , buf , sizeof(buf));
if( n > 0 && n < (int)sizeof(buf))
{
Buf
= ‘\0’;
printf("%s\n", buf);
}
}
还可以利用getcwd函数来实现。
相关文章推荐
- Linux中获取当前程序路径的方法
- C# WinForm获取当前执行程序路径的几种方法
- C#中获取程序当前路径的集中方法[转]
- C#中获取程序当前路径的集中方法收藏
- windows和linux下获取当前程序路径以及cpu数
- C#获取当前程序运行路径的方法集合//获取当前进程的完整路径
- C#获取当前程序运行路径的方法集合
- C#获取执行程序所在的当前路径的方法
- 在Linux下获取当前运行程序可执行文件全路径(不使用real_path()函数)
- C#中获取程序当前路径的集中方法[转]
- android中获取当前程序路径等方法
- Linux下获取当前程序路径
- linux下C程序获取绝对路径各种方法分析
- C#获取执行程序所在的当前路径的方法
- 关于VC获取当前程序运行路径的几种方法
- C#获取当前程序运行路径的方法集合
- vs 获取当前程序文件的路径方法
- Liunx下获取当前程序路径的方法
- linux下获取程序当前目录绝对路径
- C#获取程序当前路径的方法