首页 > 其他分享 >26_文件IO

26_文件IO

时间:2024-04-10 20:01:50浏览次数:28  
标签:文件 26 ret fd IO printf include buf

文件IO

文件描述符

​ 对于文件 IO 来说, 一切都是围绕文件操作符来进行的。 在 Linux 系统中, 所有打开的文件都有一个对应的文件描述符。

​ 文件描述符的本质是一个非负整数, 当我们打开一个文件时, 系统会给我们分配一个文件描述符。

​ 当我们对一个文件做读写操作的时候, 我们使用 open 函数返回的这个文件描述符会标识该文件,并将其作为参数传递给 read 或者 write 函数。 在 posix.1 应用程序里面, 文件描述符 0,1,2 分别对应着标准输入, 标准输出, 标准错误。

open函数

image-20240402155257092

参数 flags 可选标志:

image-20240402155312905

当参数flags=O_CREAT时,可以加上第三个参数mode表面权限

文件的权限

​ 文件的访问权限我们可以使用数字来表示:
​ 可执行 x ->1
​ 可写 w ->2
​ 可读 r ->4
​ 例:
​ 如果我们要表示可读可写,就是上述值的和,可读可写->6

代码

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

int main(int argc, char const *argv[])
{

    int fd;                                   // 文件描述符
    fd = open("a.c", O_CREAT | O_RDWR, 0666); // 不存在就创建 可读可写模式 读写权限
    if (fd < 0)
    {
        printf("open is error\n");
    }
    printf("fd is %d\n", fd);

    return 0;
}

运行结果

image-20240402162341198image-20240402162424215

发现问题: a.c的文件权限不是0666

open函数创建文件时的权限是我们设置的mode&~(umask)

image-20240402162725072

即: 0666&~(0022)

​ 110 110 110 & ~(000 010 010)

​ 110 110 110 & 111 101 101

​ 110 100 100 -> 6 4 4 -> rw - r-- r--

close函数

image-20240402171454047

代码

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

int main(int argc, char const *argv[])
{

    int fd;                                   // 文件描述符
    fd = open("a.c", O_CREAT | O_RDWR, 0666); // 不存在就创建 可读可写模式 读写权限
    if (fd < 0)
    {
        printf("open is error\n");
    }
    printf("fd is %d\n", fd);
    close(fd);
    return 0;
}

read函数

image-20240402172119201

正常读

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

int main(int argc, char const *argv[])
{

    int fd;                                   // 文件描述符
    char buf[32]={0};
    ssize_t ret;
    fd = open("a.c", O_CREAT | O_RDWR, 0666); // 不存在就创建 可读可写模式 读写权限
    if (fd < 0)
    {
        printf("open is error\n");
        return -1;
    }
    printf("fd is %d\n", fd);

    ret = read(fd, buf, sizeof(buf));
    if(ret < 0)
    {
        printf("read is error\n");
        return -2;
    }
    printf("buf is %s\n", buf);
    printf("ret is %ld\n", ret);
    close(fd);
    return 0;
}

运行结果

image-20240402172924047

生成的文件中最后多一个\n,所以一个9个字符

读到结尾再读一次

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

int main(int argc, char const *argv[])
{

    int fd;                                   // 文件描述符
    char buf[32]={0};
    ssize_t ret;
    fd = open("a.c", O_CREAT | O_RDWR, 0666); // 不存在就创建 可读可写模式 读写权限
    if (fd < 0)
    {
        printf("open is error\n");
        return -1;
    }
    // printf("fd is %d\n", fd);

    ret = read(fd, buf, sizeof(buf));
    if(ret < 0)
    {
        printf("read is error\n");
        return -2;
    }
    printf("buf is %s\n", buf);
    printf("ret is %ld\n", ret);

    ret = read(fd, buf, sizeof(buf));
    printf("ret is %ld\n", ret);

    close(fd);
    return 0;
}

运行结果

image-20240402173231443

ret返回0表示已经读到文件末尾

write函数

image-20240402173518217

代码

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

int main(int argc, char const *argv[])
{

    int fd;                                   // 文件描述符
    char *buf="hello\n";
    ssize_t ret;
    
    fd = open("a.c", O_CREAT | O_RDWR, 0666); // 不存在就创建 可读可写模式 读写权限

    if (fd < 0)
    {
        printf("open is error\n");
        return -1;
    }
    // printf("fd is %d\n", fd);
    

   write(fd, buf, sizeof(buf));

    close(fd);
    return 0;
}

运行结果

image-20240403132907496

综合练习

通过命令行操作,把a.c文件里面的内容写到b.c里面。

代码

open.c

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

int main(int argc, char const *argv[])
{
    int fd_src, fd_obj;
    ssize_t ret = 0;
    char buf[32];
    if(argc != 3)
    {
        printf("Usage:%s <src file> <obj file>\n", argv[0]);
        return -1;
    }
    fd_src = open(argv[1], O_RDONLY);
    fd_obj = open(argv[2], O_CREAT|O_WRONLY, 0666);

    while((ret = read(fd_src, buf, sizeof(buf))) != 0)
    {
        write(fd_obj, buf, ret);
    }

    close(fd_src);
    close(fd_obj);

    return 0;
}

运行结果

image-20240403140145389

lseek函数

介绍

​ 所有打开的文件都有一个当前文件偏移量(current file offset) ,以下简称为 cfo。

​ cfo 通常是一个非负整数, 用于表明文件开始处到文件当前位置的字节数。 读写操作通常开始于 cfo, 并且使 cfo 增大, 增量为读写的字节数。 文件被打开时, cfo 会被初始化为0, 除非使用了 O_APPEND 。 使用 lseek 函数可以改变文件的 cfo 。

image-20240403140603056

成功返回当前指针位置

举个例子
 把文件位置指针设置为 100 lseek(fd,100,SEEK_SET);
 把文件位置设置成文件末尾 lseek(fd,0,SEEK_END);
 确定当前的文件位置 lseek(fd,0,

标签:文件,26,ret,fd,IO,printf,include,buf
From: https://www.cnblogs.com/mzx233/p/18127285

相关文章

  • 27_目录IO
    目录IO文件IO和目录IO的对比:区别:​ 之前我们学习的文件IO和提到过的标准IO都是对文件操作,接下来学习的目录IO都是对目录操作。mkdir创建目录函数如下表所示:代码mkdir.c#include<stdio.h>#include<sys/stat.h>#include<sys/types.h>intmain(intargc,......
  • VMware配置共享文件夹
    前两天用petalinux配置ARMLinux操作系统,可能是电脑用久了性能下降,虚拟机卡死了,不得不重新安装一次。设置了共享文件夹后,点进/mnt目录发现居然是空的,很早之前就遇到过这个问题,但是时间太久了忘了怎么解决了,不得不重新上网查一查。要先安装VMwareTools,但是按钮是灰色的点不了:......
  • Minio存储文件
    Minio是一个高性能的对象存储服务器,它可以在Linux、MacOS和Windows等操作系统上运行,并通过命令行界面或RESTfulAPI进行管理。本文为用Minio存储文件。1.在pom.xml文件中添加MinIO的Java客户端库依赖<dependency><groupId>io.minio</groupId><artifactId>......
  • xshell常用命令 以及文件属性类型
      xshell常用命令1tree/home/树状形式显示yuminstalltree2cat:查看文本内容cat>>test2.txt<<EOF>ads>adf>EOF3less,more:文本查看,分页less/etc/services4head-n1/etc/services:查看该文件第一行5psaux|head-n5:查看前5......
  • python基础-数据类型、字典、集合、文件操作(打开、关闭、读写、追加等)
    前言!!!注意:本系列所写的文章全部是学习笔记,来自于观看视频的笔记记录,防止丢失。观看的视频笔记来自于:哔哩哔哩武沛齐老师的视频:​​2022Python的web开发(完整版)入门全套教程,零基础入门到项目实战​数据结构数据类型字符串列表元组集合字典整型布尔None浮点型字节类......
  • 自定义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.函数在......
  • Java登陆第四十二天——Axios拦截器
    如果想在axios发送HTTP请求之前。或者是接收响应之前做一些额外的工作,可以通过拦截器完成。Axios拦截器分为请求拦截器,响应拦截器。分别在请求或响应时生效。一图了解Axios拦截器提供了两种文本函数:名字太长,直接看语法语法格式如下://请求拦截器,f1对应请求发送成功函数,f2......
  • H.265视频直播点播录像EasyPlayer.js流媒体播放器用户常见问题及解答
    EasyPlayer属于一款高效、精炼、稳定且免费的流媒体播放器,可支持多种流媒体协议播放,无须安装任何插件,起播快、延迟低、兼容性强,使用非常便捷。今天我们来汇总下用户常见的几个问题及解答。1、EasyPlayer.js播放多路H.265视频时,CPU直接被占满该如何处理?答:因为H.265解码比较占......
  • WDS+MDT网络启动自动部署windows(三)UEFI & BIOS 双PXE引导
    简介:我们可以通过调整启动文件来兼容不同的硬件(UEFI&BIOS),能否不手动调整呢?自动调整也是可以的。本来是是想将DHCP放在H3C5500上的,但是咨询过H3C的售前顾问后,没有任何一个型号支持这个功能,前面已经折腾过自动识别客户端类型,发送不同的启动文件了。为了更好的完成这个系列文章......