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 }

 

posted on 2016-05-07 09:37  旭东的博客  阅读(21534)  评论(0编辑  收藏  举报

导航