目录IO和文件IO相差不大,主要包括对目录的打开、 关闭、 读写等操作。
1、创建目录
使用mkdir函数来创建一个目录
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
mkdir()执行成功会返回 0, 出错时返回-1。 并设置 error 值
mkdir()函数参数含义如下所示:
| 参数名称 | 参数含义 | |
| 1 | pathname | 路径和文件名 |
| 2 | mode | 权限掩码,对不同的用户和组可设置读、写、执行权限。用8进制表示,如“0777” |
实例:
#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) // 判断命令行参数是否正确, 如果不正确, 则输出提示信息并返回-1
{
printf("Usage:%s <name file>\n", argv[0]);
return -1;
}
ret = mkdir(argv[1], 0666); // 创建目录, 使用指定的权限
if (ret < 0) // 如果创建目录失败, 返回-2, 输出错误信息
{
perror("mkdir is error\n");
return -2;
}
printf("mkdir is ok\n"); // 输出创建目录成功的提示信息
return 0; // 返回 0, 表示程序运行成功
}
2、打开目录和关闭目录
在 Linux 操作系统中使用 opendir()打开指定的目录, 使用 closedir()函数关闭目录流。 所使
用的头文件和函数原型, 如下所示:
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
int closedir(DIR *dirp);
opendir 调成功返回打开的目录流, 失败返回 NULL, closedir 调用成功返回 0, 调用失败
返回-1。
两个函数的相关参数含义如下所示
| 参数名称 | 参数含义 | |
| 1 | name | 路径名字 |
| 2 | 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; // 声明一个指向 DIR 结构体的指针
if (argc != 2) // 判断命令行参数是否正确, 如果不正确, 则输出提示信息并返回-1
{
printf("Usage:%s <name file>\n", argv[0]);
return -1;
}
dp = opendir(argv[1]); // 打开指定的目录
if (dp == NULL) // 如果打开目录失败, 返回-1, 输出错误信息
{
perror("opendir is error\n");
return -1;
}
printf("opendir is ok\n"); // 输出打开目录成功的提示信息
closedir(dp); // 关闭打开的目录流
return 0; // 返回 0, 表示程序运行成功
}
3、读取目录内容
readdir()函数用于读取打开的目录中的文件和子目录, 所使用的头文件和函数原型, 如下
所示:
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
函数返回指向 dirent 类型结构体的指针, 该结构体包含了文件和子目录的信息, 如文件名、
文件类型等, 失败返回 NULL
readdir()函数参数含义如下所示:
| 参数名称 | 参数含义 | |
| 1 | dirp | 指向 DIR 类型结构体的指针 dirp |
实例:
#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)
{
perror("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)
{
perror("open is error\n");
return -2;
}
fd_obj = open(file_name, O_CREAT | O_RDWR, 0666); // 打开或创建目标文件
if (fd_obj < 0)
{
printf("open is error\n");
return -3;
}
// 步骤六: 读写操作
while ((ret = read(fd_src, buf, 32)) != 0) // 循环读取源文件
{
write(fd_obj, buf, ret); // 将读取到的数据写入目标文件
}
// 步骤七: 关闭目录和文件
close(fd_src);
close(fd_obj);
closedir(dp);
return 0;
}

被折叠的 条评论
为什么被折叠?



