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 }
执行结果:
 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM