目录操作

1.目录的权限

目录作为一种特殊的问价,同样具有访问权限,并且使用和普通文件一样的方式存储文件访问权限字。

前面讲过的用于判断文件是否用户可读,可写等的宏照样可以再这里用。

 

2.创建目录

 

#include <unistd.h>

int mkdir(const char * pathname,mode_t mode);

 

在设置目录权限时,不要忘记加上执行权限,这样其他程序才能访问到该目录的子目录

一个目录中,总有.和..这两个默认的目录项。

 

 

3.删除一个目录

 

#include <unistd.h>

int rmdir(const char * pathname);

 

该函数只能删除空的目录。

 

4.打开与关闭目录

 

#include<sys/types.h>
#include<dirent.h>
定义函数 DIR * opendir(const char * name);
函数说明 opendir()用来打开参数name指定的目录,并返回DIR*形态的目录流,和open()类似,接下来对目录的读取和搜索都要使用此返回值。
返回值 成功则返回DIR* 型态的目录流,打开失败则返回NULL。
错误代码 EACCESS 权限不足
EMFILE 已达到进程可同时打开的文件数上限。
ENFILE 已达到系统可同时打开的文件数上限。
ENOTDIR 参数name非真正的目录
ENOENT 参数name 指定的目录不存在,或是参数name 为一空字符串。
ENOMEM 核心内存不足。

 

 

#include<sys/types.h>
#include<dirent.h>
定义函数 int closedir(DIR *dir);
函数说明 closedir()关闭参数dir所指的目录流。
返回值 关闭成功则返回0,失败返回-1,错误原因存于errno 中。
错误代码 EBADF 参数dir为无效的目录流

 

 

5.读目录

 

linux下允许用户像读取问价内容一样读取目录中存放的文件的目录项。

 

#include <sys/types.h>
#include <dirent.h>
定义函数 struct dirent * readdir(DIR * dir);
函数说明 readdir()返回参数dir目录流的下个目录进入点。
结构dirent定义如下
struct dirent
{
ino_t d_ino;
ff_t d_off;
signed short int d_reclen;
unsigned char d_type;
har d_name[256];
};
d_ino 此目录进入点的inode
d_off 目录文件开头至此目录进入点的位移
d_reclen _name的长度,不包含NULL字符
d_type d_name 所指的文件类型
d_name 文件名
返回值 成功则返回下个目录进入点。有错误发生或读取到目录文件尾则返回NULL
附加说明 EBADF参数dir为无效的目录流。

 

由上面红色内容可知需要根据errno变量的值来判断readdir函数是否正确结束。

 

下例计算一个目录及子目录中所有文件的数目。

//file_count.c

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>

#define MAX 1024

/* get_file_count函数的参数是一个路径,表示目录树的根。
* 该函数返回参数指定目录下所用普通文件的数目。
*/
int get_file_count(char *root)
{
    DIR * dir;
    struct dirent * ptr;
    int total = 0; /* 文件总数 */
    char path[MAX];
   
    dir = opendir(root); /* 打开目录 */
    if(dir == NULL){
        perror("fail to open dir");
        exit(1);
    }

    errno = 0;
    while((ptr = readdir(dir)) != NULL){ /* 顺序读取每一个目录项 */
        /* 跳过".."和"."两个目录 */
        if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0)
              continue;
          if(ptr->d_type == DT_DIR){ /* 如果是目录,则递归调用get_file_count函数 */
              sprintf(path, "%s/%s", root, ptr->d_name);
              total += get_file_count(path);
          }
          if(ptr->d_type == DT_REG) /* 如果是普通文件,则总数递增 */
              total++;   
    }

    if(errno != 0){ /* 如果errno是0,则说明目录中目录项已经全部读完 */
        perror("fail to read dir");
        exit(1);
    }
   
    closedir(dir); /* 注意一定要关闭目录 */

    return total; /* 返回普通文件数 */
}

int main(int argc, char * argv[])
{
    int total;
   
    if(argc != 2){ /* 命令行参数错误 */
        printf("wrong usage/n");
        exit(1);
    }

    total = get_file_count(argv[1]); /* 得到文件数目 */

    printf("%s has %d files/n", argv[1], total);

    return 0;
}

 

 

 

6.实现自己的ls命令

 

//my_ls.c

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>

#define MAX 1024

void my_ls(char *root)
{
    DIR * dir;
    struct dirent * ptr;
    int count;
   
    dir = opendir(root); /* 打开目录 */
    if(dir == NULL){
        perror("fail to open dir");
        exit(1);
    }

    errno = 0;
    while((ptr = readdir(dir)) != NULL){ /* 顺序读取每一个目录项 */
        /* 跳过".."和"."两个目录 */
        if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0)
              continue;
          printf("%s/t", ptr->d_name); /* 打印每一个文件的文件名 */
       
        count++; /* 换行计数 */
        if(count %5 == 0)
            printf("/n");
    }
   
    if(errno != 0){ /* 如果errno是0,则说明目录中目录项已经全部读完 */
        perror("fail to read dir");
        exit(1);
    }
   
    closedir(dir); /* 注意一定要关闭目录 */
}

int main(int argc, char * argv[])
{
    if(argc != 2){
        printf("wrong usage/n");
        exit(1);
    }

    my_ls(argv[1]);

    return 0;
}

 

 

 

7.得到目录文件的读写位置

 

像普通文件一样,对目录的读写操作也会影响到目录文件的当前读写位置。

 

#include<dirent.h>
定义函数 off_t telldir(DIR *dir);
函数说明 telldir()返回参数dir目录流目前的读取位置。此返回值代表距离目录文件开头的偏移量返回值返回下个读取位置,有错误发生时返
回-1。
错误代码 EBADF参数dir为无效的目录流。

 

 

范例:

#include<sys/types.h>
#include<dirent.h>
#include<unistd.h>
main()
{
DIR *dir;
struct dirent *ptr;
int offset;
dir = opendir(“/etc/rc.d”);
while((ptr = readdir(dir))!=NULL)
{
offset = telldir (dir);
printf(“d_name : %s offset :%d/n”, ptr->d_name,offset);
}
closedir(dir);
}
执行
d_name : . offset :12
d_name : .. offset:24
d_name : init.d offset 40
d_name : rc0.d offset :56
d_name :rc1.d offset :72
d_name:rc2.d offset :88
d_name:rc3.d offset 104
d_name:rc4.d offset:120
d_name:rc5.d offset:136
d_name:rc6.d offset:152
d_name:rc offset 164
d_name:rc.local offset :180
d_name:rc.sysinit offset :4096

 

 

8.定位目录

 

目录文件中每个目录项的大小是确定的(此处是12),因此目录项也能而且也更容易实现定位。

 

#include<dirent.h>
定义函数 void seekdir(DIR * dir,off_t offset);
函数说明 seekdir()用来设置参数dir目录流目前的读取位置,在调用readdir()时便从此新位置开始读取。参数offset 代表距离目录文件开头的偏移量。
错误代码 EBADF 参数dir为无效的目录流

 

 

范例:

#include<sys/types.h>
#include<dirent.h>
#include<unistd.h>
main()
{
DIR * dir;
struct dirent * ptr;
int offset,offset_5,i=0;
dir=opendir(“/etc/rc.d”);
seekdir(dir,24);//跳过.和..
while((ptr = readdir(dir))!=NULL)
{
offset = telldir(dir);
printf(“d_name :%s offset :%d/n”,ptr->d_name.offset);
}
closedir(dir);
}

 

 

d_name : init.d offset 40
d_name : rc0.d offset :56
d_name :rc1.d offset :72
d_name:rc2.d offset :88
d_name:rc3.d offset 104
d_name:rc4.d offset:120
d_name:rc5.d offset:136
d_name:rc6.d offset:152
d_name:rc offset 164
d_name:rc.local offset :180
d_name:rc.sysinit offset :4096

 

 

9.回卷目录

 

有时候需要重新读取目录文件而又不想关闭目录文件,此时可使用如下函数,将一个已经打开的目录文件的读写位置回到目录文件的起始处。

 

#include<sys/types.h>
#include<dirent.h>
定义函数 void rewinddir(DIR *dir);
函数说明 rewinddir()用来设置参数dir 目录流目前的读取位置为原来开头的读取位置。
错误代码 EBADF dir为无效的目录流

 

范例:

//rewind.c

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>

int main(void)
{
    DIR *dir;
    struct dirent * d_ptr;

    dir=opendir("/home/admin"); /* 打开目录 */
    if(dir == NULL){
        perror("fail to open dir");
        exit(1);
    }
   
    while((d_ptr = readdir(dir))!=NULL){ /* 顺序输出目录中的每个文件的文件名 */
        printf("dir name is :%s /n",d_ptr->d_name);
    }

    printf("read again/n");
    rewinddir(dir); /* 目录文件的读写位置归零 */
   
    while((d_ptr = readdir(dir))!=NULL){ /* 再次输出每个文件 */
        printf("dir name is :%s/n",d_ptr->d_name);
    }

    closedir(dir); /* 关闭目录 */

    return 0;
}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值