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