【北京迅为】《STM32MP157开发板嵌入式开发指南》-第三十一章 目录IO

iTOP-STM32MP157开发板采用ST推出的双核cortex-A7+单核cortex-M4异构处理器,既可用Linux、又可以用于STM32单片机开发。开发板采用核心板+底板结构,主频650M、1G内存、8G存储,核心板采用工业级板对板连接器,高可靠,牢固耐用,可满足高速信号环境下使用。共240PIN,CPU功能全部引出:底板扩展接口丰富底板板载4G接口(选配)、千兆以太网、WIFI蓝牙模块HDMI、CAN、RS485、LVDS接口、温湿度传感器(选配)光环境传感器、六轴传感器、2路USB OTG、3路串口,CAMERA接口、ADC电位器、SPDIF、SDIO接口等


第三十一章 目录IO

31.6 目录IO mkdir()

文件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。

函数定义:

mkdir():创建一个目录

       #include <sys/stat.h>

       #include <sys/types.h>

       int mkdir(const char *pathname, mode_t mode);

参数含义:

pathname:路径和文件名

mode: 权限掩码,对不同用户和组设置可执行,读,写权限,使用八进制数表示,此参数可不写。

返回值:

mkdir()执行成功会返回0,出错时返回-1。

实验代码

在程序中,创建文件夹,代码如下所示:代码在配套资料“iTOP-STM32MP157开发板网盘资料汇总\09_嵌入式Linux开发指南(iTOP-STM32MP157)手册配套资料\系统编程配套程序\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,如下图所示:

31.7 目录IO opendir()/closedir() 

函数

DIR *opendir(const char *name);

头文件

#include <sys/types.h>

#include <dirent.h>

参数name

路径名字

返回值

成功返回打开的目录流,失败返回 NULL。

 

函数定义

opendir():打开指定的目录,并返回DIR*形态的目录流,

#include <sys/types.h>

        #include <dirent.h>

        DIR *opendir(const char *name);

参数含义:

name:路径名字。

返回值:成功返回打开的目录流,失败返回 NULL。

函数

int closedir(DIR *dirp)

头文件

#include <sys/types.h>

#include <dirent.h>

参数dirp

要关闭的目录流指针

函数定义:

closedir():关闭目录流。

       #include <sys/types.h>

       #include <dirent.h>

       int closedir(DIR *dirp);

参数含义

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文件夹。

31.8目录IO readdir() 

函数

struct dirent *readdir(DIR *dirp);

int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);

头文件

#include <dirent.h>

参数DIR *dirp

要读取的目录流指针

返回值

成功返回读取到的目录流指针,失败返回 NULL

函数定义

readdir():用来读一个目录

  #include <dirent.h>

struct dirent *readdir(DIR *dirp);

int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);

参数含义:

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目录下的子目录。 

31.9综合练习(二)

实验要求

在综合练习1的基础上,利用我们本阶段学习的知识,修改综合练习1的代码,增加以下需求:

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,如下图所示

编译运行如下图所示: 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值