日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

文件IO引入

發布時間:2025/4/5 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文件IO引入 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1 文件從哪里來
    • 2 怎么訪問文件
      • 2.1 通用的IO模型
      • 2.2 不是通用的函數
    • 3 系統調用怎么進入內核
    • 在這里插入圖片描述
    • 4 內核的sys_open、sys_read會做什么

1 文件從哪里來

在Linux系統中,一切都是“文件”:普通文件、驅動程序、網絡通信等等。所有的操作,都是通過“文件IO”來操作的。所以,很有必要掌握文件操作的常用接口。

文件一般從如下來:

  • 磁盤、Flash、SD卡、U盤,這些都是真實文件,是以某種格式(FAT32、EXT4等)保存在某個設備上(/dev/xxxx),要先mount。
  • Linux內核提供的虛擬文件系統,也要先mount。
  • 特殊文件:/dev/xxx,設備節點(字符設備、塊設備),FIFO,socket。

  • 2 怎么訪問文件

    2.1 通用的IO模型

    通用的IO模型有:open/read/write/lseek/close。示例代碼如下:

    #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h>/** ./copy 1.txt 2.txt* argc = 3* argv[0] = "./copy"* argv[1] = "1.txt"* argv[2] = "2.txt"*/ int main(int argc, char **argv) {int fd_old, fd_new;char buf[1024];int len;/* 1. 判斷參數 */if (argc != 3) {printf("Usage: %s <old-file> <new-file>\n", argv[0]);return -1;}/* 2. 打開老文件 */fd_old = open(argv[1], O_RDONLY);if (fd_old == -1){printf("can not open file %s\n", argv[1]);return -1;}/* 3. 創建新文件 */fd_new = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);if (fd_new == -1){printf("can not creat file %s\n", argv[2]);return -1;}/* 4. 循環: 讀老文件-寫新文件 */while ((len = read(fd_old, buf, 1024)) > 0){if (write(fd_new, buf, len) != len){printf("can not write %s\n", argv[2]);return -1;}}/* 5. 關閉文件 */close(fd_old);close(fd_new);return 0; }

    2.2 不是通用的函數

    有些函數不是通用的函數:ioctl/mmap。示例代碼如下:

    #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <sys/mman.h>/** ./copy 1.txt 2.txt* argc = 3* argv[0] = "./copy"* argv[1] = "1.txt"* argv[2] = "2.txt"*/ int main(int argc, char **argv) {int fd_old, fd_new;struct stat stat;char *buf;/* 1. 判斷參數 */if (argc != 3) {printf("Usage: %s <old-file> <new-file>\n", argv[0]);return -1;}/* 2. 打開老文件 */fd_old = open(argv[1], O_RDONLY);if (fd_old == -1){printf("can not open file %s\n", argv[1]);return -1;}/* 3. 確定老文件的大小 */if (fstat(fd_old, &stat) == -1){printf("can not get stat of file %s\n", argv[1]);return -1;}/* 4. 映射老文件 */buf = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd_old, 0);if (buf == MAP_FAILED){printf("can not mmap file %s\n", argv[1]);return -1;}/* 5. 創建新文件 */fd_new = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);if (fd_new == -1){printf("can not creat file %s\n", argv[2]);return -1;}/* 6. 寫新文件 */if (write(fd_new, buf, stat.st_size) != stat.st_size){printf("can not write %s\n", argv[2]);return -1;}/* 7. 關閉文件 */close(fd_old);close(fd_new);return 0; }

    3 系統調用怎么進入內核

    4 內核的sys_open、sys_read會做什么


    參考資料:

  • 韋東山全系列視頻第1季快速入門
  • 總結

    以上是生活随笔為你收集整理的文件IO引入的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。