文件和目录之函数mkdir、mkdirat、rmdir和读目录函数 您所在的位置:网站首页 linux命令mkdir怎么读 文件和目录之函数mkdir、mkdirat、rmdir和读目录函数

文件和目录之函数mkdir、mkdirat、rmdir和读目录函数

2024-06-22 12:59| 来源: 网络整理| 查看: 265

用mkdir和mkdirat函数来创建目录,用rmdir函数删除目录。

#include int mkdir(const char *pathname, mode_t mode); int mkdirat(int fd, const char *pathname, mode_t mode); 两个函数返回值:若成功,返回 0;若失败,返回 -1

mkdir和mkdirat函数在fd指定为AT_FDCWD或pathname参数指定为绝对路径名时两者是完全一样的。否则,fd参数是一个打开目录,相对路径名根据此打开目录进行计算。

测试示例

void test1(){ if(mkdir("mkdirfile", 0775) < 0) err_sys("mkdir error."); printf("mkdir success.\n"); } void test2(){ if(mkdirat(AT_FDCWD, "mkdiratfile", 0775) < 0) err_sys("mkdirat error."); int fd; printf("mkdirat success.\n"); if((fd=open("../exercise", O_RDONLY)) < 0) err_sys("open error."); if(mkdirat(fd, "mkdiratfile2", 0755) < 0) err_sys("mkdirat error."); printf("mkdirat success.\n"); close(fd); }

结果如下:

通过上图可知,在当前目录下mkdir创建了mkdirfile目录,当mkdirat函数fd参数指定为AT_FDCWD时,与mkdir函数完全一样,mkdiratfile则是在当前目录下由mkdirat函数创建的目录;当fd参数指定为../exercise(即为打开一个目录)时,则相对路径名依此进行计算并在创建了../exercise下创建了mkdiratfile2。

 

用rmdir函数可以删除一个空目录,空目录即为只包含.和..的目录。

#include int rmdir(const char *pathname); 函数返回值: 若成功,返回 0; 若出错,返回 -1。

测试示例:

void test3(){ if(rmdir("mkdirfile") < 0) err_sys("rmdir error."); printf("rmdir success.\n"); }

结果如下:

由图可知mkdirfile目录被删除。

 

对某个目录具有访问权限的任一用户都可以读该目录,但是为了防止文件系统产生混乱,只有内核才能写目录。

#include DIR *opendir(const char *pathname); DIR *fdopendir(int fd); 两个函数返回值:若成功,返回 0;若出错,返回NULL。 struct dirent *readdir(DIR *dp); 返回值:若成功,返回 0;若在目录尾或出错,返回NULL。 void rewinddir(DIR *dp); int closedir(DIR *dp); 返回值:若成功,返回 0;若出错,返回 -1。 long telldir(DIR *dp); 返回值:与dp关联的目录中的当前位置 void seekdir(DIR *dp, long loc);

dirent结构体定义在头文件中,具体结构与实现有关。实现对此结构所做的定义至少包含以下两个成员:

ino_t d_ino; // i-node number char d_name[256]; // null-terminated filename

该结构体在glibc中的具体实现如下:

struct dirent{ ino_t d_ino; // inode number off_t d_off; // not an offset 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]; // null-terminated filename };

小试牛刀,我们来使用上述目录操作编写一个简单的统计目录下个文件个数的程序如下:

#include "../../include/apue.h" #include #include /* * function type that is called for each filename * */ typedef int Myfunc(const char *, const struct stat *, int); static Myfunc myfunc; static int myftw(char *, Myfunc *); static int dopath(Myfunc *); static long nreg, nblk, ndir, nchr, nfifo, nslink, nsock, ntot; int main(int argc, char *argv[]) { int ret; if(argc != 2) err_quit("usage: ftw "); ret = myftw(argv[1], myfunc); ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock; if(ntot == 0) ntot = 1; printf("total = %7ld\n", ntot); printf("regular files = %7ld, %5.2f %%\n", nreg, nreg*100.0/ntot); printf("directories = %7ld, %5.2f %%\n", ndir, ndir*100.0/ntot); printf("block special = %7ld, %5.2f %%\n", nblk, nblk*100.0/ntot); printf("char special = %7ld, %5.2f %%\n", nchr, nchr*100.0/ntot); printf("FIFOs = %7ld, %5.2f %%\n", nfifo, nfifo*100.0/ntot); printf("symbolic links = %7ld, %5.2f %%\n", nslink, nslink*100.0/ntot); printf("sockets = %7ld, %5.2f %%\n", nsock, nsock*100.0/ntot); return 0; } /* * Descend through the hierarchy, starting at "pathname". * The caller's func() is called for every file. * */ #define FTW_F 1 #define FTW_D 2 #define FTW_DNR 3 #define FTW_NS 4 static char *fullpath; static size_t pathlen; static int myftw(char *pathname, Myfunc *func) { fullpath = path_alloc(&pathlen); if(pathlen


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有