目录相关函数

获取当前目录

linux命令中可以使用pwd查看当前目录,系统编程中可以通过
getcwd获取当前目录
man 3 getcwd
char *getcwd(char *buf, size_t size);
参数*buf:保存当前目录的缓冲区
参数size:在现代linux 中,buf 的长度至少可以为255 字节
返回值:成功返回指向当前目录的指针,和buf 的值一样,错误返回NULL

char *getwd(char *buf);该函数已经过时,使用的时候会有警告
参数*buf:保存当前目录的缓冲区
返回值:成功返回指向当前目录的指针,和buf 的值一样,错误返回NULL


char *get_current_dir_name(void);
参数:无
返回值:成功返回指向当前目录的指针,错误返回NULL
编写编译运行测试

#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
        char buf[255];
        char *wd;

        if(!getcwd(buf, sizeof(buf))) {
                perror("getcwd");
                return 1;
        }

        printf("getcwd is %s
", buf);

        wd = getwd(buf);
        if(!wd) {
                perror("getwd");
                return 1;
        }

        printf("getwd is %s
", buf);

        wd = get_current_dir_name();
        if(!wd) {
                perror("get_current_dir_name");
                return 1;
        }

        printf("get_current_dir_name is %s
", wd);
        return 0;
}

mkdir创建目录

linux命令中可以使用mkdir创建目录,系统编程中可以通过mkdir创建目录
man mkdir
int mkdir(const char *pathname, mode_t mode);
参数:文件路径
参数mode:直接使用数字即可。和前面命令中chmod 777 xxx 中的777 这个参数含义类似,也可以使用文档中的组合值。
返回值:成功返回0,错误返回-1。
编写编译运行测试

#include <sys/stat.h>
#include <sys/types.h>

#include <stdio.h>

int main(int argc, char *argv[])
{
        int ret;
        if(argc < 2) {
                printf("
Please input path
");
                return 1;
        }

        ret = mkdir(argv[1], 0777);
        if(ret < 0) {
                printf("mkdir %s failed
", argv[1]);
                return 1;
        }

        printf("mkdir %s success!
", argv[1]);

        return 0;
}

rmdir删除目录

linux命令中可以使用rmdir删除目录,系统编程中可以通过rmdir删
除目录
man rmdir
int rmdir(const char *pathname);
参数*pathname:文件和目录的路径
返回值:成功返回0,错误返回-1
编写编译运行测试

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

int main(int argc, char *argv[])
{
        int ret;
        if(argc < 2) {
                printf("Please input remove dir
");
                return 1;
        }

        ret = rmdir(argv[1]);
        if(ret < 0) {
                printf("rmdir %s failed
", argv[1]);
                return 0;
        }

        printf("rmidr %s success!
", argv[1]);
        return 0;
}

chdir改变当前目录

在实际应用中,代码需要从当前目录进到其他目录,完成操作,然后再回到当前目录。这个时候需要getcwd获取当前目录路径,保存起来,再使用chdir跳转到其他目录,然后再使用chidr和保存的路径到最初的目录

int chdir(const char *path);

-- 参数*path:文件路径

-- 返回值: 成功返回0, 错误返回-1

int fchdir(int fd);

-- 参数fd:open函数返回的句柄,文件描述符

-- 返回值:成功返回0,错误返回-1

编写编译运行测试,进入目录删除目录下一个文件夹,后返回原来目录

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

#define LENGTH 255

int main(int argc, char *argv[])
{
        int ret;
        char pwd[LENGTH];
//检测参数
        if(argc<3) {
                printf("
Please input file path
");
                return 1;
        }

//getcwd函数后去当前目录
        if(!getcwd(pwd, LENGTH)) {
                perror("getcwd");
                return 1;
        }
        printf("
getcwd pwd is %s
", pwd);

//使用chidr函数转入目录
        ret = chdir(argv[1]);
        if(ret) {
                printf("
Please input file path
");
                return 1;
        }
        printf("chdir %s is success!
", argv[1]);

        ret = rmdir(argv[2]);
        if(ret < 0) {
                printf("rmdir %s failed!
", argv[2]);
                return 1;
        }
        printf("rmdir %s is success!
", argv[2]);

        ret = chdir(pwd);
        if(ret) {
                printf("Please make sure file path
");
                return 1;
        }
        printf("chdir %s is success!
", pwd);

        return 0;
}

opendir和closedir目录

前面介绍open和close函数用于打开关闭文件,这里介绍的opendir
和closedir用于打开目录,相当于ls命令
man 3 opendir
DIR *opendir(const char *name);
参数:目录的路径。
返回值:成功返回指向目录流的指针,错误返回NULL
int closedir(DIR *dirp);
参数:opendir 返回的dir 指针
返回值:成功返回0, 失败返回-1
编写编译运行测试

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int main(int argc, char *argv[])
{
        int ret;
        DIR *dir;

        if(argc < 2) {
                printf("
Please input file path
");
                return 1;
        }

        dir = opendir(argv[1]);
        if(dir == NULL) {
                printf("opendir %s failed
", argv[1]);
                return 1;
        }
        printf("opendir %s success!
", argv[1]);

        closedir(dir);

        return 0;
}

readdir读取目录信息

在前面使用opendir打开目录的基础上,可以使用readdir读取目录信

man 3 readdir
读目录信息函数
struct dirent *readdir(DIR *dirp);
参数dirp:opendir 函数打开目录后返回的文件指针。
返回值:成功返回指向dirp 的指针dirent ,错误返回NULL。
编写编译运行测试

#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>

int main(int argc, char *argv[])
{
        int ret;
        DIR *dir;
        struct dirent *catlog;

        if(argc < 2) {
                printf("Please input file path
");
                return 1;
        }

        dir = opendir(argv[1]);
        if(dir == NULL) {
                printf("opendir %s failed!
", argv[1]);
                return 1;
        }

        catlog = readdir(dir);
        if(catlog == NULL) {
                printf("readdir %s failed!
", argv[1]);
                return 1;
        }

        printf("dir inode is %ld
", catlog->d_ino);

        closedir(dir);
        return 0;
}
Readdir
无欲速,无见小利。欲速,则不达;见小利,则大事不成。
原文地址:https://www.cnblogs.com/ch122633/p/9402118.html