針對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 }
執行結果:
