Linux中获取当前程序路径的方法
来源:互联网 发布:传智java视频百度云 编辑:程序博客网 时间:2024/06/14 11:14
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[n]= ‘\0’;
printf("%s\n", buf);
}
}
还可以利用getcwd函数来实现。
- Linux中获取当前程序路径的方法
- C#中获取程序当前路径的集中方法[转]
- C#中获取程序当前路径的集中方法[转]
- C#中获取程序当前路径的集中方法
- C#中获取程序当前路径的方法
- C#中获取程序当前路径的集中方法
- C#中获取程序当前路径的方法
- C#中获取程序当前路径的方法
- C#中获取程序当前路径的集中方法收藏
- C#中获取程序当前路径的集中方法
- C#中获取程序当前路径的集中方法
- C#中获取程序当前路径的方法
- 【转载】C#中获取程序当前路径的集中方法
- C#中获取程序当前路径的集中方法
- C#中获取程序当前路径的几种方法
- C#中获取程序当前路径的集中方法
- C#中获取程序当前路径的集中方法 :
- C#获取程序当前路径的方法
- HPUX-交换分区
- VMware 下redhat 9.0内核升级(2.4.20-8到2.6.18)
- 关于做网站
- 在centos安装多个Mysql数据库
- 图像编程----使用CheckboxGroup 和只使用checkbox区别
- Linux中获取当前程序路径的方法
- 新视图 pushViewController presentModalViewController 用法
- oracle更换表空间 blob字段带来的问题
- JS去除两边空格
- android组件——BroadCastReceiver(简例)
- 关于抱怨
- c语言基础(三)之数据交换
- sizeof问题
- mysql忘记密码