i.MX8MM处理器采用了先进的14LPCFinFET工艺,提供更快的速度和更高的电源效率;四核Cortex-A53,单核Cortex-M4,多达五个内核 ,主频高达1.8GHz,2G DDR4内存、8G EMMC存储。千兆工业级以太网、MIPI-DSI、USB HOST、WIFI/BT、4G模块、CAN、RS485等接口一应俱全。H264、VP8视频硬编码,H.264、H.265、VP8、VP9视频硬解码,并提供相关历程,支持8路PDM接口、5路SAI接口、2路Speaker。系统支持Android9.0(支持获取root限)Linux4.14.78+Qt5.10.1、Yocto、Ubuntu20、Debian9系统。适用于智能充电桩,物联网,工业控制,医疗,智能交通等,可用于任何通用工业和物联网应用、
【公众号】迅为电子
【粉丝群】258811263(加群获取驱动文档+例程)
第三十二章 目录IO
32.1 目录IO mkdir()
本章内容对应视频讲解链接(在线观看):
目录I0之mkdir函数 → https://www.bilibili.com/video/BV1zV411e7Cy?p=10
文件IO和目录IO的对比:
区别:
之前我们学习的文件IO和提到过的标准IO都是对文件操作,接下来学习的目录IO都是对目录操作。创建目录函数如下表所示:
函数 | int mkdir(const char *pathname, mode_t mode); |
头文件 | #include <sys/stat.h> #include <sys/types.h> |
参数pathname | 路径和文件名 |
参数mode | 权限掩码,对不同用户和组设置可执行,读,写权限,使用八进制数表示,此参数可不写。 |
返回值 | mkdir()执行成功会返回0,出错时返回-1。 |
功能 | 创建一个目录 |
实验代码
在程序中,创建文件夹,代码如下所示:代码在配套资料“iTOP-i.MX8MM开发板\02-i.MX8MM开发板网盘资料汇总(不含光盘内容)\嵌入式Linux开发指南(iTOP-i.MX8MM)手册配套资料\1.系统编程例程\系统编程配套程序\linux\08”目录下。
#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,如下图所示:
32.2 目录IO opendir()/closedir()
本章内容对应视频讲解链接(在线观看):
目录IO之opendirflclosedi函数 → https://www.bilibili.com/video/BV1zV411e7Cy?p=11
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 | 要关闭的目录流指针 |
功能 | 关闭目录流。 |
实验代码
在程序中,打开指定目录。代码在配套资料“iTOP-i.MX8MM开发板\02-i.MX8MM开发板网盘资料汇总(不含光盘内容)\嵌入式Linux开发指南(iTOP-i.MX8MM)手册配套资料\1.系统编程例程\系统编程配套程序\linux\09”目录下。
#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文件夹。
32.3 目录IO readdir()
本章内容对应视频讲解链接(在线观看):
目录I0之readdir函数 → https://www.bilibili.com/video/BV1zV411e7Cy?p=12
读一个目录使用函数 readdir(),详解如下表所示:
函数 | struct dirent *readdir(DIR *dirp); int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result); |
头文件 | #include <dirent.h> |
参数DIR *dirp | 要读取的目录流指针 |
返回值 | 成功返回读取到的目录流指针,失败返回 NULL |
功能 | 用来读一个目录 |
实验代码
在程序中,读取目录。代码在配套资料“iTOP-i.MX8MM开发板\02-i.MX8MM开发板网盘资料汇总(不含光盘内容)\嵌入式Linux开发指南(iTOP-i.MX8MM)手册配套资料\1.系统编程例程\系统编程配套程序\linux\10”目录下。
#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);}elsebreak;}closedir(dp);return 0;
}
运行测试
编译程序,如下图所示:
运行程序如下图所示,读取到了test目录下的子目录。
32.4 综合练习(二)
本章内容对应视频讲解链接(在线观看):
综合练习(二) → https://www.bilibili.com/video/BV1zV411e7Cy?p=13
实验要求
在综合练习1的基础上,利用我们本阶段学习的知识,修改综合练习1的代码,增加以下需求:
1.打印我们要拷贝的目录下的所有文件名,并拷贝我们需要的文件。
2.通过键盘输入我们要拷贝的文件的路径和文件名等信息
实验代码
代码在配套资料“iTOP-i.MX8MM开发板\02-i.MX8MM开发板网盘资料汇总(不含光盘内容)\嵌入式Linux开发指南(iTOP-i.MX8MM)手册配套资料\1.系统编程例程\系统编程配套程序\linux\11”目录下。
#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);}elsebreak;}// 步骤四:获得文件的名字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,如下图所示
编译运行如下图所示