• 如何理解Linux文件IO?


    一、文件IO的概述
    1、什么是文件?
    Linux下一切皆文件。普通文件、目录文件、管道文件、套接字文件、链接文件、字符设备文件、块设备文件。
    2、什么是IO?
    input  output:输入输出
    3、什么是文件IO?
    对文件的输入输出,把数据写入文件/从文件中读取数据
    系统IO:系统调用的IO接口。(open、close、read、write、lseek、mmap、munmap)
    标准IO:基于标准C库的IO接口
    二、系统IO函数的应用
    Linux下的man手册的使用:man  man
    第一节:查可执行程序:man 1 ls
    第二节:查系统调用的函数:man 2 open
    第三节:查库调用的函数:man 3 printf

    - open  打开文件
    #include #include #include

    int open(const char *pathname, int flags);  int open(const char *pathname, int flags, mode_t mode); 返回值类型:int
    返回值:
    打开文件成功,返回一个新的文件描述符,>=0(相当于人的身份证号)
    打开文件失败,返回-1
    形参:
    pathname:要打开的文件的路径名
    flags:打卡文件的方式
    O_RDONLY  只读
    O_WRONLY 只写
    O_RDWR     可读可写
    以上三种方式互斥
    O_APPEND:以追加方式打开文件。
    O_CREAT:如果要打开的文件不存在,系统就创建该文件并打开。
    O_TRUNC:如果要打开的文件中已有数据,那就打开文件并清除已有的数据。
    mode:用于指定新建文件的权限,用八进制表示。

    - close 关闭文件函数
     #include   int close(int fd); 返回值类型:int
    返回值:
    关闭文件成功,返回0
    关闭文件失败,返回-1
    形参
    fd:要关闭的文件的文件描述符

    练习:编写代码,实现在共享目录中,打开1.txt文件,并打印出文件描述符,再关闭文件。

    1. #include <sys/types.h>
    2. #include <sys/stat.h>
    3. #include <fcntl.h>
    4. #include <unistd.h>
    5. #include <stdio.h>
    6. int main()
    7. {
    8. //打开文件
    9. int fd1;
    10. fd1 = open("./1.txt",O_RDWR);
    11. if(fd1 == -1)
    12. {
    13. printf("open 1.txt failed!\n");
    14. return -1;
    15. }
    16. printf("fd1 = %d\n",fd1);
    17. close(fd1);
    18. return 0;
    19. }
    20. /*
    21. fd1 = 3;
    22. 文件描述符是从3开始的,为什么会这样?不是说文件描述符>=0?
    23. 其实系统会默认打开三个标准流控,012就会被占用
    24. 0 ---> 标准输入 stdin
    25. 1 ---> 标准输出 stdout
    26. 2 ---> 标准错误 stderr
    27. 所以当我们自己用open函数打开文件时,文件描述符是从3开始的
    28. */


    - lseek   偏移文件指针

    1.  #include
    2.  #include
    3.  
    4.  off_t lseek(int fd, off_t offset, int whence); 返回值类型:off_t (整形)


    返回值:
             偏移成功,返回偏移字节数
             偏移失败,返回-1
    形参一:fd  文件描述符
    形参二:偏移量
    形参三:偏移位置
             SEEK_SET   从头位置开始偏移
             SEEK_CUR  从当前位置开始偏移
             SEEK_END  从末尾位置开始偏移
    练习2:编写代码,实现在1.txt文件中写入"hello world",再从该文件中读取5个字节数据,并打印出来。

    1. #include <sys/types.h>
    2. #include <sys/stat.h>
    3. #include <fcntl.h>
    4. #include <unistd.h>
    5. #include <stdio.h>
    6. int main(void)
    7. {
    8.     //1.打开文件
    9.     int fd = open("./1.txt", O_RDWR|O_CREAT);
    10.    
    11.     if (fd == -1)
    12.     {
    13.         printf("open  1.txt failed!\n");
    14.         return -1;
    15.     }
    16.     //2.写入数据
    17.     char wr_buf[15] = "hello world";
    18.     write(fd, wr_buf, 11);    
    19.     //3.读取数据
    20.     char rd_buf[15] = {0};
    21.     lseek(fd, 0, SEEK_SET);
    22.     read(fd, rd_buf,5);
    23.     
    24.     printf("%s\n", rd_buf);
    25.     
    26.     //4.关闭文件
    27.     close(fd);
    28.    
    29.     return 0;
    30. }

    作业:实现一个简单文件拷贝功能,将文件1的数据拷贝到文件2,如果文件2不存在则创建,如果文件2中已有数据则清除。

  • 相关阅读:
    设计模式之职责链模式应用例题
    贪心算法(区间问题)
    CentOS 7 - Linux 安装详解
    ffmpeg知识点整理
    flutter 应用签名 Mac
    Linux | vim的入门手册
    vue2+axios实现修改和删除element-ui表格数据
    docker镜像打包上传阿里云镜像仓库
    数据库索引详解
    Windows安装双版本MySQL
  • 原文地址:https://blog.csdn.net/m0_64148419/article/details/136771135