linux的getcwd和readlink的区别

针对linux下的程序,有两个路径: 1>运行程序的路径; 2>可执行文件所在的路径

例如:

如果我在/home/yongchao下执行

$ ./temp/test    那么 运行程序的路径是:/home/yongchao   而可执行文件所在的路径是/home/yongchao/test

执行结果:

 

ps: 还有一个通过相对路径来寻找绝对路径的程序

 1 #include<limits.h>
 2 #include<stdlib.h>
 3 #include<string>
 4 #include<iostream>
 5 using namespace std;
 6 
 7 int main(int argc, char** argv)
 8 {
 9     char relative_path[1024] = "./";
10     char absolute_path[1024] = {0};
11     if ( NULL == realpath(relative_path, absolute_path) )
12     {
13         cout<<"resolve path error"<<endl;
14         return 0;
15     }
16 
17     cout<<"the absolute path : "<<absolute_path<<endl;
18     return 0;
19 }
执行结果:

 

posted @ 2016-06-01 16:26  哈哈不是嘎嘎  阅读(2527)  评论(0编辑  收藏  举报