目录
前言:
文件 IO 和目录 IO 的对比:
目录IO | 文件IO |
opendir创建目录 | open打开文件 |
mkdir创建目录 | |
readdir读目录 | read读文件 |
closedir关闭目录 | close关闭文件 |
区别:
之前我们学习的文件 IO 和提到过的标准 IO 都是对文件操作,接下来学习的目录 IO 都是对目录操作。
目录 IO
1.目录 IO mkdir()
创建目录函数如下表所示:
函数
|
int mkdir(const char *pathname, mode_t mode);
|
头文件
|
#include <sys/stat.h>
#include <sys/types.h>
|
参数 pathname
|
路径和文件名
|
参数 mode
|
权限掩码,对不同用户和组设置可执行,读,写权限,使用八进制数表示,此参数可不写。
|
返回值
|
mkdir() 执行成功会返回 0 ,出错时返回 -1 。
|
功能
|
创建一个目录
|
实验代码
在程序中,创建文件夹,代码如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{
int ret;
if (argc != 2)
{
printf("Usage:%s <name file>\n", argv[0]);
return -1;
}
ret=mkdir(argv[1],0666);
if(ret<0)
{
printf("mkdir is error\n");
}
printf("mkdir is ok\n");
return 0;
}
}
编译运行
在 Ubuntu 上编译文件,并运行程序,成功创建文件夹 test ,如下图所示:

2.目录 IO opendir()/closedir()
opendir 和 closedir 函数详解如下所示:
函数
|
DIR *opendir(const char *name);
|
头文件
|
#include <sys/types.h>
#include <dirent.h>
|
参数 name
|
路径名字
|
返回值
|
成功返回打开的目录流,失败返回 NULL 。
|
功能
|
打开指定的目录,并返回 DIR* 形态的目录流
|
函数
|
int closedir(DIR *dirp)
|
头文件
|
#include <sys/types.h>
#include <dirent.h>
|
参数 dirp
|
要关闭的目录流指针
|
功能
|
关闭目录流。
|
实验代码
在程序中,打开指定目录。
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
int ret;
DIR *dp;
if (argc != 2)
{
printf("Usage:%s <name file>\n", argv[0]);
return -1;
}
dp = opendir(argv[1]);
if (dp != NULL)
{
printf("opendir is ok\n");
return -1;
}
closedir(dp);
return 0;
}
编译执行,即可看到目录下的文件,新建文件夹 test,并运行命令打开 test 文件夹。

3.目录 IO readdir()
读一个目录使用函数 readdir() ,详解如下表所示:
函数
|
struct dirent *readdir(DIR *dirp);
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
|
头文件
|
#include <dirent.h>
|
参数 DIR *dirp
|
要读取的目录流指针
|
返回值
|
成功返回读取到的目录流指针,失败返回 NULL
|
功能
|
用来读一个目录
|
实验代码
在程序中,读取目录。
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
int ret;
DIR *dp;
struct dirent *dir;
if (argc != 2)
{
printf("Usage:%s <name file>\n", argv[0]);
return -1;
}
dp = opendir(argv[1]);
if (dp == NULL)
{
printf("opendir is error\n");
return -2;
}
printf("opendir is ok\n");
while (1)
{
dir = readdir(dp);
if (dir != NULL)
{
printf("file name is %s\n", dir->d_name);
}
else
break;
}
closedir(dp);
return 0;
}
运行测试
编译程序,如下图所示:

运行程序如下图所示,读取到了 test 目录下的子目录。

4.综合练习
实验要求
在上节博客综合练习 的基础上,利用我们本阶段学习的知识,修改该综合练习 的代码,增加以下需求:
1.打印我们要拷贝的目录下的所有文件名,并拷贝我们需要的文件。
2.通过键盘输入我们要拷贝的文件的路径和文件名等信息
实验代码
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
int main(int argc, char *argv[])
{
//步骤一:定义变量
int fd_src;
int fd_obj;
char buf[32] = {0};
char file_path[32] = {0};
char file_name[32] = {0};
ssize_t ret;
struct dirent *dir;
DIR *dp;
// 步骤二:从键盘输入文件路径
printf("Please enter the file path:\n");
scanf("%s", file_path);
// 步骤三:打开目录,获得目录流指针,并读取目录
dp = opendir(file_path);
if (dp == NULL)
{
printf("opendir is error\n");
return -1;
}
printf("opendir is ok\n");
while (1)
{
dir = readdir(dp);
if (dir != NULL)
{
printf("file name is %s\n", dir->d_name);
}
else
break;
}
// 步骤四:获得文件的名字
printf("Please enter the file name:\n");
scanf("%s", file_name);
// 步骤五:获得文件描述符
fd_src = open(strcat(strcat(file_path, "/"), file_name), O_RDWR);
if (fd_src < 0)
{
printf("open is error\n");
return -1;
}
fd_obj = open(file_name, O_CREAT | O_RDWR, 0666);
if (fd_obj < 0)
{
printf("open is error\n");
return -2;
}
// 步骤六:读写操作
while ((ret = read(fd_src, buf, 32)) != 0)
{
write(fd_obj, buf, ret);
}
// 步骤七:关闭目录,文件
close(fd_src);
close(fd_obj);
closedir(dp);
return 0;
}
在 Ubuntu 上首先新建 test 文件夹,文件夹里面新建三个文件 ;a.c,b.c,c.c ,如下图所示

编译运行如下图所示:
