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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux系统编程 | 01 -文件操作

發布時間:2024/3/13 linux 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux系统编程 | 01 -文件操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、文件操作方法

linux中有兩種方法可以操作文件:系統調用和c庫函數。

1. 什么是系統調用?

由操作系統實現并提供給外部應用程序的編程接口(API),是應用程序同系統之間數據交互的橋梁。

C標準函數和系統函數調用關系,如圖:

2. c庫函數

待寫。

二、文件IO(系統調用API)

庫函數頭文件統一使用頭文件unistd.h。

1. open創建

頭文件:

#include <fcntl.h>

函數原型:

int open(const char *pathname, int flags);

參數說明:

  • pathname:文件名
  • flag:文件打開標志
flag參數說明
O_RDONLY只讀方式打開
O_WRONLY只寫方式打開
O_RDWR可讀可寫
O_CREAT文件不存在則創建(文件權限由mode參數指定)
O_APPEND追加方式寫入
O_EXCL判斷文件是否存在
O_TRUNC截斷文件大小為0/清空文件
O_NONBLOCK

mode權限參數說明:

  • 該參數使用八進制指定;
  • 該參數受umask(默認0002)影響:最終權限 = mode &~ umask,其實就是把我們所指定權限中other用戶的寫權限給去除。
  • 返回值說明:

    • 成功返回fd號
    • 失敗返回-1,錯誤原因errno給出

    2. close關閉

    頭文件:

    #include <unistd.h>

    函數原型:

    int close(int fd);

    參數說明:

    • fd:文件描述符

    3. write寫入

    頭文件:

    #include <unistd.h>

    函數原型:

    ssize_t write(int fd, const void *buf, size_t count);

    參數說明:

    • fd:文件描述符
    • buf:待寫數據指針
    • count:待寫數據大小

    返回值:

    • 成功:返回寫入的字節數
    • 失敗:返回-1,error被設置

    4. read讀取

    頭文件:

    #include <unistd.h>

    函數原型:

    ssize_t read(int fd, void *buf, size_t count);

    參數說明:

    • fd:文件描述符
    • buf:緩沖區指針
    • count:要讀取的數據大小

    返回值:

    • 成功:返回讀取的字節數
    • 失敗:返回-1,error被設置

    說明:

    在支持查找的文件上,讀取操作從文件偏移量開始,并且文件偏移量會隨著讀取遞增。如果文件偏移量在文件末尾或者超過文件末尾,則返回0。

    5. lseek文件偏移

    頭文件:

    #include <unistd.h>

    函數原型:

    off_t lseek(int fd, off_t offset, int whence);

    參數說明:

    • fd:文件描述符
    • offset:偏移量
    • count:要讀取的數據大小
      • SEEK_SET:The file offset is set to offset bytes.
      • SEEK_CUR:The file offset is set to its current location plus offset bytes.
      • SEEK_END:The file offset is set to the size of the file plus offset bytes.

    返回值:

    • 成功:返回當前所在偏移量
    • 失敗:返回-1,error被設置

    三、測試程序

    demo1——默認創建文件的權限

    #include <stdio.h> #include <unistd.h> #include <fcntl.h>int main(int argc, char *argv[]) {int fd;fd = open("hello.txt", O_RDWR | O_CREAT);printf("fd is %d\n", fd);close(fd);return 0; }

    執行結果:

    fd is 3

    如果hello.txt不存在,則open會創建該文件,但我們并沒有指定文件權限(mode),查看默認創建文件的權限:

    ---Srwx--T 1 ubuntu ubuntu 0 Sep 5 16:40 hello.txt

    后續文件權限分析?

    demo2——指定創建文件的權限

    #include <stdio.h> #include <unistd.h> #include <fcntl.h>int main(int argc, char *argv[]) {int fd;// 0644: rw-r--r--fd = open("hello.txt", O_RDWR | O_CREAT, 0644);printf("fd is %d\n", fd);close(fd);return 0; }

    執行之后再次查看hello.txt的權限:

    rw-r--r-- 1 ubuntu ubuntu 0 Sep 5 16:47 hello.txt

    demo3——指定創建文件的權限受umask影響

    #include <stdio.h> #include <unistd.h> #include <fcntl.h>int main(int argc, char *argv[]) {int fd;// 0777: rwxrwxrwx// umask: 0002fd = open("hello.txt", O_RDWR | O_CREAT, 0777);printf("fd is %d\n", fd);close(fd);return 0; }

    創建出的hello.txt權限為:

    -rwxrwxr-x 1 ubuntu ubuntu 0 Sep 5 17:03 hello.txt*

    demo4-打開文件出錯

    #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <string.h>int main(int argc, char *argv[]) {int fd;fd = open("hello.txt", O_RDWR);printf("fd is %d\n", fd);printf("errno is %d(%s)\n", errno, strerror(errno));close(fd);return 0; }

    當hello.txt不存在時,日志為:

    fd is -1 errno is 2(No such file or directory)

    demo5——文件偏移量導致讀取失敗

    #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <string.h>int main(int argc, char *argv[]) {int fd;int nbytes;char write_buf[] = "helloworld!";char read_buf[1024] = {0};/* 1. open file */fd = open("hello.txt", O_RDWR | O_CREAT, 0664);if (fd < 0) {printf("open fail, fd is %d\n", fd);printf("errno is %d(%s)\n", errno, strerror(errno));return -1;}/* 2. write */nbytes = write(fd, write_buf, sizeof(write_buf));if (nbytes < 0) {printf("write fail, errno is %d(%s)\r\n", errno, strerror(errno));return -1;}printf("write %d bytes\r\n", nbytes);/* 3. read */nbytes = read(fd, read_buf, sizeof(read_buf));if (nbytes < 0) {printf("read fail, errno is %d(%s)\r\n", errno, strerror(errno));return -1;}printf("read %d bytes:[%s]\r\n", nbytes, read_buf);/* 4. close */close(fd);return 0; }

    write寫入完成后,文件偏移量在文件末尾,直接讀取導致返回0:

    write 12 bytes read 0 bytes:[]

    這個時候在read之前,使用lseek函數將文件偏移量恢復到文件開始處即可:

    lseek(fd, 0, SEEK_SET);

    總結

    以上是生活随笔為你收集整理的Linux系统编程 | 01 -文件操作的全部內容,希望文章能夠幫你解決所遇到的問題。

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