当前位置: 技术问答>linux和unix
linux 下如何获得当前路径
来源: 互联网 发布时间:2016-02-22
本文导语: getcwd 是获得执行程序的当前路径。 比如 可执行程序a 在root下,a里写getcwd()方法, 我在etc下执行 ./root/a 这样获得的是etc的目录, 我想在其他目录调用程序a,getcwd返回的还是a程序的当前目录。 要用什么...
getcwd 是获得执行程序的当前路径。 比如 可执行程序a 在root下,a里写getcwd()方法, 我在etc下执行 ./root/a 这样获得的是etc的目录, 我想在其他目录调用程序a,getcwd返回的还是a程序的当前目录。 要用什么方法啊 ?
|
在程序开始用chdir(const char *path)函数改变当前目录为你想要的目录,以后就可以用getcwd返回你想要的目录了
|
#include
#include
#include
#include
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("%sn",buf);
}
自己写的.改改应该就可以用
#include
#include
#include
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("%sn",buf);
}
自己写的.改改应该就可以用
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。