Linux下编程求绝对路径和读取目录

来源:互联网 发布:百度云总显示网络异常 编辑:程序博客网 时间:2024/06/05 09:03

在Linux下面编程在编写文件读写或者发送文件的时候需要知道文件的绝对路径,或者我们想通过程序来查看路径下有哪些文件,这就需要我们在程序中借助一些函数来帮我们实现这类功能,下面我们分别来看看如何求绝对路径和读取目录:

1、求绝对路径:

我们在当前目录下知道的是相对目录,再没有其他的任何信息,所以我们只能借助当前目录来完成求绝对路径了,但是我们已知的是相对路径怎么通过程序来获取文件的绝对路径呢?这里有一个函数可以做到,下面我们来看看:

 #include <limits.h> #include <stdlib.h> char *realpath(const char *path,char*resolved_path); //相对路径为path,绝对路径将放在resolved_path所指空间 //两个参数都是char*类型

return value:
If there is no error, realpath() returns a pointer to the
resolved_path.Otherwise, it returns a NULL pointer, the contents of the array resolved_path are undefined, and errno is set to indicate the error.
返回值:如果相对路径和参数给的没有错将返回的是相对路径所在的绝对路径,否则返回的是一个NULL。
下面我们来看看这函数的用法:
这里写图片描述
这里的c_str函数为了与c语言兼容,在c语言中没有string类型,是c++ 中 string类 (class) 的 函数,它能把 string类 的对象里的字符串 转换成 C 中 char 型变量 的 字符串。
运行结果:
这里写图片描述

2、读取目录:

为了让我们在程序中可以对目录进行产看,系统设置了一个函数方便我们调用来读取目录,下面我们来看看这个函数:

#include <dirent.h>struct dirent *readdir(DIR *dirp);int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);//这个函数的功能和上面的那个一样,多的功能就是重入 struct dirent {         ino_t  d_ino;/* inode number */         off_t  d_off;/* not an offset; see NOTES */         unsigned short d_reclen;/* length of this record */         unsigned char  d_type;/* type of file; not supported by all filesystem types */         char d_name[256]; /* filename */           };

至于这里的DIR类型我们来找找看:

#include <sys/types.h>#include <dirent.h>DIR *opendir(const char *name);DIR *fdopendir(int fd);

可以看到这两个函数的返回值是DIR类型,所以我们这里也就用一下这两个函数,这两个函数的都是用来打开目录的,我们看看man手册里说的:The opendir() and fdopendir() functions return a pointer to the direc‐tory stream. On error, NULL is returned, and errno is set appropri‐ately.
文件类型:

 DT_BLK      This is a block device. DT_CHR      This is a character device. DT_DIR      This is a directory. DT_FIFO     This is a named pipe (FIFO). DT_LNK      This is a symbolic link. DT_REG      This is a regular file. DT_SOCK     This is a UNIX domain socket. DT_UNKNOWN  The file type is unknown.

通过相对路径读取绝对路径:

  1 #include <sys/types.h>                                                                                                                  2 #include <dirent.h>  3 #include <dirent.h>  4 #include <unistd.h>  5   6 #include <iostream>  7 using namespace std;  8   9 int main() 10 { 11    DIR *dir =  opendir("./"); 12    dirent* pent = NULL; 13    while(NULL != (pent = readdir(dir))) 14    { 15        switch(pent->d_type) 16        { 17            case  DT_BLK: 18                cout<<"this is block file:>"<<pent->d_name<<endl; 19                break; 20  21            case  DT_CHR: 22                cout<<"this is char file:>"<<pent->d_name<<endl; 23                break; 24            case DT_DIR: 25                cout<<"this is direction file:>"<<pent->d_name<<endl; 26                break; 27            case DT_FIFO: 28                cout<<"this is fifo file:>"<<pent->d_name<<endl; 29                break; 30            case DT_LNK: 31                cout<<"this is link file:>"<<pent->d_name<<endl; 32                break; 33            case DT_REG: 34                cout<<"this is regular file:>"<<pent->d_name<<endl; 35                break; 36            case DT_SOCK: 37                cout<<"this is socke tfile:>"<<pent->d_name<<endl; 38                break; 39            case DT_UNKNOWN: 40                cout<<"this is noconfigur file:>"<<pent->d_name<<endl; 41                break; 42        } 43    } 44     return 1;                                                                                                                          45 }struct dirent *readdir(DIR *dirp);DIR *opendir(const char *name);

这里写图片描述
对当前目录进行读取:

        当期目录:

这里写图片描述

这里写图片描述

目录的其他操作函数,用到的时候可以使用man手册查看,在函数的括号里已经表明了手册序号:
getdents(2), read(2), closedir(3), dirfd(3), ftw(3), offsetof(3), opendir(3), rewinddir(3), scandir(3), seekdir(3), telldir(3)

阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 包颈手术后睡中会勃起怎么办 武警义务兵训练不合格怎么办 空姐体检心率不齐怎么办 空姐体检差身高怎么办 锻炼过猛肌肉疼怎么办 中考体检有问题怎么办 想参军视力不够怎么办 公司体检查出乙肝怎么办 乙肝携带者福利体检怎么办 小孩没考好我该怎么办 客户考虑我该怎么办 明天中考 我该怎么办 考试考差了怎么办 没考上理想高中怎么办 我是差学生中考怎么办 衬衫后背鼓起来怎么办 高考体检表没了怎么办 高考体检视力不合格怎么办 高考体检转氨酶高怎么办 高考体检肝功能异常怎么办 高考体检有乙肝怎么办 高考体检有纹身怎么办 学校体检血压高怎么办 高考体检不属实怎么办 艺考身上有花臂怎么办 义务兵因病致残怎么办 新兵练成绩优秀怎么办 阿提拉部队得了瘟疫怎么办 公安体能测评胖子怎么办 社区工作者笔试不及格怎么办 警察考核体能差怎么办 胖子跑步跑不动怎么办 电脑跑不动了怎么办 在部队体能不好怎么办 自考考试没过怎么办 没工作怎么办日本签证 毕业证被扣了怎么办 毕业证被撕了怎么办 中专的学历认证怎么办 搬宿舍东西多怎么办 员工宿舍丢东西怎么办