【Linux系统编程学习】Linux系统IO函数(open、read、write、lseek)
此為牛客網(wǎng)Linux C++課程1.20課程筆記。
1.open函數(shù)
open函數(shù)有兩種,分別是打開(kāi)一個(gè)已經(jīng)存在的文件和創(chuàng)建并打開(kāi)一個(gè)不存在的文件。
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>// 打開(kāi)一個(gè)已經(jīng)存在的文件 int open(const char *pathname, int flags); // 創(chuàng)建并打開(kāi)一個(gè)不存在的文件 int open(const char *pathname, int flags, mode_t mode);參數(shù):
- pathname:要打開(kāi)的文件路徑
- flags:對(duì)文件的操作權(quán)限設(shè)置還有其他的設(shè)置
- mode:八進(jìn)制的數(shù),表示創(chuàng)建出的新的文件的操作權(quán)限,比如:0775
flags有三個(gè)必選項(xiàng):O_RDONLY, O_WRONLY, O_RDWR 。
分別是只讀、只寫(xiě)和讀寫(xiě),這三個(gè)設(shè)置是互斥的。
flags還有一些非必選項(xiàng):
O_CREAT 如果文件不存在則創(chuàng)建該文件
O_EXCL 如果使用O_CREAT選項(xiàng)且文件存在,則返回錯(cuò)誤消息
O_NOCTTY 如果文件為終端,那么終端不可以調(diào)用open系統(tǒng)調(diào)用的那個(gè)進(jìn)程的控制終端
O_TRUNC 如果文件已經(jīng)存在則刪除文件中原有數(shù)據(jù)
O_APPEND 以追加的方式打開(kāi)
必選項(xiàng)和非必選項(xiàng)可以組合使用,中間用 | 隔開(kāi),如O_RDWR | O_CREAT | O_TRUNC。
mode參數(shù)用一個(gè)八進(jìn)制數(shù)與取反后的當(dāng)前進(jìn)程的umask掩碼進(jìn)行按位與,得到新文件的操作權(quán)限。
返回值:返回一個(gè)新的文件描述符,如果調(diào)用失敗,返回-1。
2. 關(guān)于errno
errno是Linux系統(tǒng)函數(shù)里面的一個(gè)全局變量,記錄的是最近一次錯(cuò)誤的錯(cuò)誤號(hào)。
在/usr/include/asm-generic/errno.h和/usr/include/asm-generic/errno-base.h中記錄了定義了這些錯(cuò)誤號(hào),如圖:
一個(gè)錯(cuò)誤號(hào)對(duì)應(yīng)一個(gè)錯(cuò)誤信息。
我們可以用perror函數(shù)來(lái)打印errno對(duì)應(yīng)的錯(cuò)誤信息
#include <stdio.h> void perror(const char *s);//作用:打印errno對(duì)應(yīng)的錯(cuò)誤描述s參數(shù):用戶描述,比如hello,最終輸出的內(nèi)容是 hello:xxx(實(shí)際的錯(cuò)誤描述)。
如以下示例程序,打開(kāi)一個(gè)不存在的文件a.txt
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h>int main() {// 打開(kāi)一個(gè)文件int fd = open("a.txt", O_RDONLY);if(fd == -1) {perror("open file");}// 關(guān)閉close(fd);return 0; }輸出的結(jié)果是:
是s參數(shù)+實(shí)際錯(cuò)誤描述的組合。
3. read和write函數(shù)
read函數(shù):
#include <unistd.h> ssize_t read(int fd, void *buf, size_t count);參數(shù):
- fd:文件描述符,open得到的,通過(guò)這個(gè)文件描述符操作某個(gè)文件
- buf:需要讀取數(shù)據(jù)存放的地方,數(shù)組的地址(傳出參數(shù))
- count:指定的數(shù)組的大小
返回值:一個(gè)整數(shù),有以下情況:
- 成功:
>0: 返回實(shí)際的讀取到的字節(jié)數(shù)
=0:文件已經(jīng)讀取完了 - 失敗:-1 ,并且設(shè)置errno。
write函數(shù):
#include <unistd.h> ssize_t write(int fd, const void *buf, size_t count);參數(shù):
- fd:文件描述符,open得到的,通過(guò)這個(gè)文件描述符操作某個(gè)文件
- buf:要往磁盤(pán)寫(xiě)入的數(shù)據(jù),數(shù)據(jù)
- count:要寫(xiě)的數(shù)據(jù)的實(shí)際的大小
返回值:
- 成功:實(shí)際寫(xiě)入的字節(jié)數(shù)
- 失敗:返回-1,并設(shè)置errno
示例程序如下,該程序能夠讀入一個(gè)文件中的全部?jī)?nèi)容,復(fù)制到一個(gè)新創(chuàng)建的文件中:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h>int main() {int fdread = open("english.txt", O_RDONLY);if(fdread == -1) {perror("open english.txt");return -1;}int fdwrite = open("copy.txt", O_WRONLY | O_CREAT, 0664);if(fdwrite == -1) {perror("create and open aftercopy.txt");return -1;}char buffer[1024] = {0};int len = 0;while((len = read(fdread, buffer, sizeof(buffer))) > 0) {write(fdwrite, buffer, len);}close(fdwrite);close(fdread);return 0; }4. lseek函數(shù)
#include <sys/types.h> include <unistd.h> off_t lseek(int fd, off_t offset, int whence);參數(shù):
- fd:文件描述符,通過(guò)open得到的,通過(guò)這個(gè)fd操作某個(gè)文件
- offset:偏移量
- whence:以下三選一
SEEK_SET
設(shè)置文件指針的偏移量
SEEK_CUR
設(shè)置偏移量:當(dāng)前位置 + 第二個(gè)參數(shù)offset的值
SEEK_END
設(shè)置偏移量:文件大小 + 第二個(gè)參數(shù)offset的值
返回值:返回文件指針的位置
該函數(shù)主要有以下作用:
擴(kuò)展文件長(zhǎng)度的示例程序如下:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h>int main() {int fd = open("hello.txt", O_RDWR);if(fd == -1) {perror("open");return -1;}// 擴(kuò)展文件的長(zhǎng)度int ret = lseek(fd, 100, SEEK_END);if(ret == -1) {perror("lseek");return -1;}// 寫(xiě)入一個(gè)空數(shù)據(jù)write(fd, " ", 1);// 關(guān)閉文件close(fd);return 0; }總結(jié)
以上是生活随笔為你收集整理的【Linux系统编程学习】Linux系统IO函数(open、read、write、lseek)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Linux系统编程学习】 文件描述符
- 下一篇: 卵巢早衰想做卵巢移植术能生肓