首页 > 其他分享 >27_目录IO

27_目录IO

时间:2024-04-10 20:00:12浏览次数:15  
标签:27 return file int fd IO printf include 目录

目录IO

文件 IO 和目录 IO 的对比:

image-20240404155211741

区别:

​ 之前我们学习的文件 IO 和提到过的标准 IO 都是对文件操作, 接下来学习的目录 IO 都是对目录操作。

mkdir

创建目录函数如下表所示:

image-20240404155248246

代码

mkdir.c

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

int main(int argc, char const *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");
        return -2;
    }
    printf("mkdir is ok!!!\n");
    return 0;
}

运行结果

image-20240404160954108

opendir/closedir

image-20240404161041536

代码

mkdir.c

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

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

    dp = opendir(argv[1]);

    if(dp != NULL)
    {
        printf("opendir is ok!!!\n");
    }
    
    closedir(dp);
    return 0;
}

运行结果

image-20240404161823854

readdir

image-20240404162032234

代码

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

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

    dp = opendir(argv[1]);

    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;
        }
        
    }

    closedir(dp);

    return 0;
}

运行结果

image-20240404163500779

综合练习

需求:

​ 1.打印我们要拷贝的目录下的所有文件名, 并拷贝我们需要的文件。
​ 2.通过键盘输入我们要拷贝的文件的路径和文件名等信息

代码

practice.c

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

int main(int argc, char const *argv[])
{
    DIR *dir;
    struct dirent *rdir;
    int fd_src, fd_obj;
    ssize_t ret;
    char buf[100] = {0};
    char file_path[32]={0};
    char file_name[32]={0};

    printf("please enter the file path:\n");
    scanf("%s", file_path);

    dir = opendir(file_path);
    if (dir == NULL)
    {
        printf("opendir is error!!!\n");
        return -2;
    }
    printf("opendir is ok!!!\n");
    while (1)
    {
        rdir = readdir(dir);
        if (rdir != NULL)
        {
            printf("filename: %s\n", rdir->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_RDONLY);
    fd_obj = open(file_name, O_CREAT | O_WRONLY, 0666);

    if (fd_src < 0 || 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_obj);
    close(fd_src);
    closedir(dir);

    return 0;
}

运行结果

image-20240404183857209

标签:27,return,file,int,fd,IO,printf,include,目录
From: https://www.cnblogs.com/mzx233/p/18127286

相关文章

  • Minio存储文件
    Minio是一个高性能的对象存储服务器,它可以在Linux、MacOS和Windows等操作系统上运行,并通过命令行界面或RESTfulAPI进行管理。本文为用Minio存储文件。1.在pom.xml文件中添加MinIO的Java客户端库依赖<dependency><groupId>io.minio</groupId><artifactId>......
  • 自定义Python实用函数-返回指定目录及其子目录和指定文件扩展名的文件清单列表
    importosdefget_files(file_path,image_types_set=()):"""返回指定目录及其子目录下、指定文件扩展名的文件清单列表。若image_types_set参数为空,则返回图片文件清单列表。若image_types_set参数为['.*'],则返回所有文件清单列表。"""filenames......
  • Rust的函数__Function
    FunctionsareprevalentinRustcode.You’vealreadyseenoneofthemostimportantfunctionsinthelanguage:the main function,whichistheentrypointofmanyprograms.You’vealsoseenthe fnkeyword,whichallowsyoutodeclarenewfunctions.函数在......
  • 操作系统综合题之“采用二级页表的分页存储管理方式,计算页目录号的位数 和 页大小,给定
    一、问题:某计算机系统的主存按字节编址,逻辑地址和物理地址都是32位,其内存管理采用两级页表的分页存储管理方式。逻辑地址中页号位10位,页内偏移地址为10位。该计算机系统的两级页表结构如下图所示,图中数值均为十进制数1.页目录号的位数为多少?页的大小为多少KB?2.如果页目录项大小......
  • Java登陆第四十二天——Axios拦截器
    如果想在axios发送HTTP请求之前。或者是接收响应之前做一些额外的工作,可以通过拦截器完成。Axios拦截器分为请求拦截器,响应拦截器。分别在请求或响应时生效。一图了解Axios拦截器提供了两种文本函数:名字太长,直接看语法语法格式如下://请求拦截器,f1对应请求发送成功函数,f2......
  • WDS+MDT网络启动自动部署windows(三)UEFI & BIOS 双PXE引导
    简介:我们可以通过调整启动文件来兼容不同的硬件(UEFI&BIOS),能否不手动调整呢?自动调整也是可以的。本来是是想将DHCP放在H3C5500上的,但是咨询过H3C的售前顾问后,没有任何一个型号支持这个功能,前面已经折腾过自动识别客户端类型,发送不同的启动文件了。为了更好的完成这个系列文章......
  • Air Conditioner 题解
    [AirConditioner]题意简述题目链接。给定一个整数\(n\),每秒钟可以选择使\(n\)增加\(1\)或减少\(1\)或不改变,有\(M\)个询问,对于第\(i\)个询问,给定\(t_i,l_i,r_i\),表示询问在第\(t_i\)秒时,是否有\(n\in[l_i,r_i]\)。如果能满足所有的询问,输出YES,否则输出NO。......
  • Oracle分析函数- count()/sum() over(partition by 分组 order by 排序) 详解
    优点:代码简单明了,并且执行效率高,(不影响总的记录数)如果不用这种函数去写,按照平时我们的思路首先想到的可能是子查询,那么将至少会走4次以上的全表扫描:(1)每个订单中产品数量大于3的产品至少1个(003,004)(2)每个订单中折扣标志为'1'的产品至少有2个(002,004)(3)每个订单......
  • IOS开发Archives打包后构建版本发布到TestFlight全流程
    前言:构建版本之前一定要先配置好项目icons,不然会报错。1.选择需要构建的包之后,点击右侧的DistributeApp按钮:2.Selectamethodofdistribution界面,选择AppStoreConnect(要发布到TestFlight需要选这个)3.Selectadestination——选择Upload(如果选择Export,则需要自己用......
  • 基于istio实现单集群地域故障转移
    本文分享自华为云社区《基于istio实现单集群地域故障转移》,作者:可以交个朋友。一背景随着应用程序的增长并变得更加复杂,微服务的数量也会增加,失败的可能性也会增加。微服务的故障可能多种原因造成,例如硬件问题、网络延迟、软件错误,甚至人为错误。故障转移Failover是系统韧性设......