Linux操作文件夹文件

Linux C下:操作文件夹和操作文件有些类似,操作文件之前学习的是<stdio.h>文件下。fopen这个函数,我并没有写出自己的文档。先写这个吧,,读取遍历文件夹。

1. 文件夹遍历读取

头文件 #include <dirent.h>中,如下:
下面读取文件夹要用到结构体struct dirent,

#include <dirent.h>
struct dirent
{
   long d_ino;     /* inode    number 索引节点号 */
   off_t d_off;    /* offset to this dirent   在目录文件中的偏移 */
   unsigned short d_reclen;     /* length of this d_name 文件名长 */
   unsigned char d_type;        /* the type of d_name 文件类型 */
   char d_name [NAME_MAX+1];    /* file name (null-terminated) 文件名,最长255字符 */
}
其中d_type表明该文件的类型:文件(8)、目录(4)、链接文件(10)等。

下面程序,递归读取某文件夹及其子文件夹下所有文件名:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>    //文件夹头文件
#include <unistd.h>

int readFileList(char *basePath)   //阅读文件列表
{
    DIR *dir;           //看到DIR 就像我第一次看见FIFO的感觉。
    struct dirent *ptr;
    char base[1000];

    if ((dir=opendir(basePath)) == NULL)
    {
        perror("Open dir error...");
        exit(1);
    }

    while ((ptr=readdir(dir)) != NULL)
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)  //文件名是.      ..代表什么意思?
            continue;
        else if(ptr->d_type == 8)    //file
            printf("d_name:%s/%s\n",basePath,ptr->d_name);
        else if(ptr->d_type == 10)    //link file
            printf("d_name:%s/%s\n",basePath,ptr->d_name);
        else if(ptr->d_type == 4)    //dir  文件夹再次循环套娃。
        {
            memset(base,'\0',sizeof(base));
            strcpy(base,basePath);
            strcat(base,"/");
            strcat(base,ptr->d_name);
            readFileList(base);
        }
    }
    closedir(dir);
    return 1;
}

int main(void)
{
    DIR *dir;
    char basePath[1000];
    memset(basePath,'\0',sizeof(basePath));
    getcwd(basePath, 999);
    printf("the current dir is : %s\n",basePath);
    readFileList(basePath);
    return 0;
}

在这里插入图片描述
//上面的文件中还有一部分是不理解的,basepath这个意义是什么?
//我还是不够清楚,这里需要理解一下。

2.文件操作中几个函数

看Linux下文件操作相关章节,遇到了这么几个结构体,仔细研究了一下。

首先说说DIR这一结构体,以下为DIR结构体的定义:

struct __dirstream
   {
    void *__fd;
    char *__data;
    int __entry_data;
    char *__ptr;
    int __entry_ptr;
    size_t __allocation;
    size_t __size;
    __libc_lock_define (, __lock)
   };
typedef struct __dirstream DIR;

DIR结构体类似于FILE,是一个内部结构,以下几个函数用这个内部结构保存当前正在被读取的目录的有关信息(摘自《UNIX环境高级编程(第二版)》)
函数 DIR *opendir(const char *pathname),即打开文件目录,返回的就是指向DIR结构体的指针,
而该指针由以下几个函数使用:

1 struct dirent *readdir(DIR *dp);
2 void rewinddir(DIR *dp);
3 int closedir(DIR *dp);
4 long telldir(DIR *dp);
5 void seekdir(DIR *dp,long loc);

关于DIR结构,我们知道这么多就可以了,没必要去再去研究他的结构成员。

————————————————————————————————————————————

接着是dirent结构体,首先我们要弄清楚目录文件(directory file)的概念:
这种文件包含了其他文件的名字以及指向与这些文件有关的信息的指针
(摘自《UNIX环境高级编程(第二版)》)。
从定义能够看出,dirent不仅仅指向目录,还指向目录中的具体文件,readdir函数同样也读取目录下的文件,这就是证据。以下为dirent结构体的定义:

struct dirent
{
  long d_ino; /* inode number 索引节点号 */
    off_t d_off; /* offset to this dirent 在目录文件中的偏移 */
    unsigned short d_reclen; /* length of this d_name 文件名长 */
    unsigned char d_type; /* the type of d_name 文件类型 */
    char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */
}

从上述定义也能够看出来,dirent结构体存储的关于文件的信息很少,所以dirent同样也是起着一个索引的作用,如果想获得类似ls -l那种效果的文件信息,必须要靠stat函数了。

通过readdir函数读取到的文件名存储在结构体dirent的d_name成员中,而函数

int stat(const char *file_name, struct stat *buf);

的作用就是获取文件名为d_name的文件的详细信息,存储在stat结构体中。以下为stat结构体的定义:

struct stat {
        mode_t     st_mode;       //文件访问权限
        ino_t      st_ino;       //索引节点号
        dev_t      st_dev;        //文件使用的设备号
        dev_t      st_rdev;       //设备文件的设备号
        nlink_t    st_nlink;      //文件的硬连接数
        uid_t      st_uid;        //所有者用户识别号
        gid_t      st_gid;        //组识别号
        off_t      st_size;       //以字节为单位的文件容量
        time_t     st_atime;      //最后一次访问该文件的时间
        time_t     st_mtime;      //最后一次修改该文件的时间
        time_t     st_ctime;      //最后一次改变该文件状态的时间
        blksize_t st_blksize;    //包含该文件的磁盘块的大小
        blkcnt_t   st_blocks;     //该文件所占的磁盘块
      };

这个记录的信息就很详细了吧,呵呵。

最后,总结一下,想要获取某目录下(比如a目下)b文件的详细信息,我们应该怎样做?

首先,我们使用opendir函数打开目录a,返回指向目录a的DIR结构体c。

接着,我们调用readdir( c)函数读取目录a下所有文件(包括目录),返回指向目录a下所有文件的dirent结构体d。

然后,我们遍历d,调用stat(d->name,stat *e)来获取每个文件的详细信息,存储在stat结构体e中。

总体就是这样一种逐步细化的过程,在这一过程中,三种结构体扮演着不同的角色。

d_type表示档案类型:

enum
{
    DT_UNKNOWN = 0,         //未知类型
# define DT_UNKNOWN DT_UNKNOWN
    DT_FIFO = 1,            //管道
# define DT_FIFO DT_FIFO
    DT_CHR = 2,             //字符设备
# define DT_CHR DT_CHR
    DT_DIR = 4,             //目录
# define DT_DIR DT_DIR
    DT_BLK = 6,             //块设备
# define DT_BLK DT_BLK
    DT_REG = 8,             //常规文件
# define DT_REG DT_REG
    DT_LNK = 10,            //符号链接
# define DT_LNK DT_LNK
    DT_SOCK = 12,           //套接字
# define DT_SOCK DT_SOCK
    DT_WHT = 14             //链接
# define DT_WHT DT_WHT
};


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值