LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1059|回复: 4

如何得到程序所在的路径

[复制链接]
发表于 2006-3-6 10:03:13 | 显示全部楼层 |阅读模式
有一些数据放在程序所在的一个子目录里

有什么C函数吗?
getcwd好像是返回当前路径的,
发表于 2006-3-6 11:52:58 | 显示全部楼层
[PHP]
char buff[256];
int pid;

pid = getpid();
sprintf(buff, "/proc/%d/exe", pid);
readlink(buff, tmp_name, sizeof(tmp_name));
p = strrchr(tmp_name, '/');
*p = 0;
[/PHP]
buff就是程序所在路径
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-6 12:03:10 | 显示全部楼层
谢谢X11
你这名字太短了-----不说帖子发不出去.
回复 支持 反对

使用道具 举报

发表于 2006-3-6 17:59:35 | 显示全部楼层
我也想问这个问题,被你先了,我尝试过从main的参数得到,结果没有成功,还有QApplication类里面也有个得到应用程序路径的方法,但是得到的字串是不对的,我想应该是操作系统提供的函数,C语言里面是没有的,我不知道如何查Linux系统的API函数,郁闷中。。。。。
回复 支持 反对

使用道具 举报

发表于 2006-3-7 13:57:45 | 显示全部楼层
some lines in unistd.h
[php]
/* Get the pathname of the current working directory,
   and put it in SIZE bytes of BUF.  Returns NULL if the
   directory couldn't be determined or SIZE was too small.
   If successful, returns BUF.  In GNU, if BUF is NULL,
   an array is allocated with `malloc'; the array is SIZE
   bytes long, unless SIZE == 0, in which case it is as
   big as necessary.  */
extern char *getcwd (char *__buf, size_t __size) __THROW;

#ifdef        __USE_GNU
/* Return a malloc'd string containing the current directory name.
   If the environment variable `PWD' is set, and its value is correct,
   that value is used.  */
extern char *get_current_dir_name (void) __THROW;
#endif

#if defined __USE_BSD || defined __USE_XOPEN_EXTENDED
/* Put the absolute pathname of the current working directory in BUF.
   If successful, return BUF.  If not, put an error message in
   BUF and return NULL.  BUF should be at least PATH_MAX bytes long.  */
extern char *getwd (char *__buf) __THROW __nonnull ((1));
#endif
[/php]
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表