Linux系统编程 | 01 -文件操作
生活随笔
收集整理的這篇文章主要介紹了
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:文件打開標志
| O_RDONLY | 只讀方式打開 |
| O_WRONLY | 只寫方式打開 |
| O_RDWR | 可讀可寫 |
| O_CREAT | 文件不存在則創建(文件權限由mode參數指定) |
| O_APPEND | 追加方式寫入 |
| O_EXCL | 判斷文件是否存在 |
| O_TRUNC | 截斷文件大小為0/清空文件 |
| O_NONBLOCK |
mode權限參數說明:
返回值說明:
- 成功返回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.txtdemo3——指定創建文件的權限受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 -文件操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图片从前端回传到后端实现思路总结
- 下一篇: VTK保存渲染图片