linux C++ 获取文件绝对路径


提供ftp服务时需要获取文件绝对路径,这里记录一下。

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 #include <limits.h>
 4 
 5 int main(){
 6     const char *file_name = "filename";
 7     char abs_path_buff[PATH_MAX];
 8 
 9     //获取文件路径, 填充到abs_path_buff
10     //realpath函数返回: null表示获取失败; 否则返回指向abs_path_buff的指针
11     if(realpath(file_name, abs_path_buff)){
12         printf("%s %s\n", file_name, abs_path_buff);
13     }
14     else{
15         printf("the file '%s' is not exist\n", file_name);    
16     }
17 
18     return 0;
19 }

 


免责声明!

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



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