linux 系統函數調用:open close read write lseek

  1. open函數
    查看函數原型 man 2 open
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

相關參數用法介紹;

a.	pathname 文件名
b. flags必選項:O_RDONLY 只讀O_WRONLY 只寫O_RDWR   讀寫可選項:O_APPEND 追加O_CREAT  創建文件O_EXCL 與 O_CREAT 一起使用,如果文件存在,則報錯mode 權限位,最終(mode & ~umask)O_NONBLOCK 非阻塞返回值:返回最小的可用文件描述符,失敗返回-1, 設置errno
  1. close 函數
    man 2 close
 #include <unistd.h>int close(int fd);

參數介紹: fd為要關閉的文件描述符
返回值:成功返回0, 失敗返回-1, 設置errno

ps:C語言參數使用 | 可以有多項參數的實現原理,實際上就是位符
比如:int 類型 32個位,哪幾個位代表那幾個含義

  1. read函數
    man 2 read
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
參數介紹:fd 文件描述符buf 緩沖區count 緩沖區大小
返回值:失敗返回-1,設置errno成功返回讀到的大小0代表讀到文件末尾非阻塞情況下:read返回-1,但是此時需要判斷errno的值,看是否是因為非阻塞的原因導致返回-1

非阻塞情況下:
read返回-1,但是此時需要判斷errno的值,看是否是因為非阻塞的原因導致返回-1
這里回頭再學習以下 ???
代碼示例,使用read函數實現 linux cat命令的功能

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
int main(int arg, char *argv[]) {if(arg != 2)  {printf("./a.out filename\n");return -1;}int fd = ope?n(argv[1], O_RDONLY);if(fd == -1) {printf("not found filename\n");return -1;}char buf[256];int read_ret = read(fd, buf, sizeof(buf));while (read_ret > 0){printf("%s", buf);memset(buf, '\0', sizeof(buf));read_ret = read(fd, buf, sizeof(buf));  }close(fd);return 0;
}

注意使用這一行代碼memset(buf, ‘\0’, sizeof(buf));
不然最后一次輸出buf,會出現多余的情況 ,暨每次使用完buf,都清洗一次。

  1. wirte
#include <unistd.h>       
ssize_t write(int fd, const void *buf, size_t count);
參數介紹:fd 文件描述符buf 緩沖區count 緩沖區大小
返回值:成功,返回寫入的字節數失敗,返回-1,設置errno0, 代表未寫入
  1. lseek函數
    移動文件讀寫位置
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
參數介紹:fd文件描述符offset 偏移量whence SEEK_SET 文件開始位置SEEK_CUR 當前位置SEEK_END 結尾返回值:成功:返回當前位置到開始的長度失敗:返回-1,設置errno

以下代碼示例:
將一個字符串helloworld,寫到一個文件里面,讀取出來,輸出在屏幕上

de <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>int main(int argc, char *argv[]) {if(argc != 2) {printf("./a.out filename1");return -1;}int fd = open(argv[1], O_RDWR|O_CREAT, 0666);char *w_temp = "helloworld";int length = strlen(w_temp);int write_ret = write(fd, w_temp, length);if(write_ret>0) {printf("write bytes of %d\n", length);} else {printf("write error\n");return -1;}char r_temp[256];int seek_ret = lseek(fd, 0, SEEK_SET);if(seek_ret == -1) {printf("lseek error\n");return -1;}memset(r_temp, '\0', sizeof(r_temp));int read_ret = read(fd, r_temp, sizeof(r_temp));printf("%d\n", read_ret);if(read_ret !=-1){printf("read bytes of %d\n", strlen(r_temp));printf("read is\n");printf("%s\n", r_temp);} else {printf("read error\n");return -1;}write(STDOUT_FILENO, r_temp, strlen(r_temp));close(fd);return 0;
}

注意

int seek_ret = lseek(fd, 0, SEEK_SET);

將文件讀寫位置,重寫設置為文件開始,不然以后的代碼讀取不到文件的內容。

lseek函數計算文件大小
代碼示例:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int main(int argc, char *argv[]) {if(argc !=2) {printf("./a.out filename1\n");return -1;}int fd = open(argv[1], O_RDONLY);if(!fd) {printf("open error\n");return -1;}int file_size = lseek(fd, 0, SEEK_END);printf("%s file size is %d\n", argv[1], file_size);close(fd);return 0;
}

lseek 拓展文件
以下代碼,創建一個新文件, 并且使其大小為1024字節。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>int main(int argc, char *argv[]) {if(argc!=2) {printf("./a.out filename1\n");return -1;}int fd = open(argv[1], O_WRONLY|O_CREAT, 0666);int l_ret = lseek(fd, 1023, SEEK_END);char a[1] = {'a'};// 這里必須要寫一次,才能生效int w_ret = write(fd, a, 1); // or write(fd, "a", 1);close(fd);return 0;
}

代碼舉例:

  1. 同一個進程中,兩次打開同一個文件,進行寫操作
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>int main(int argc, char *argv[]) {if(argc != 2) {printf("./a.out filename1\n");return -1;}int o_ret = open(argv[1], O_RDWR|O_CREAT, 0666);printf("o_ret = %d\n", o_ret);int o_ret1 = open(argv[1], O_RDWR|O_CREAT, 0666);printf("o_ret1 = %d\n", o_ret1);write(o_ret, "hello", 5);lseek(o_ret1, 5, SEEK_CUR);// 這里注意lseek的用法,不然,下面的world,會把上面的hello覆蓋掉write(o_ret1, "world", 5);int cl_ret = close(o_ret);int cl_ret1 = close(o_ret1);printf("cl_ret = %d\n", cl_ret);printf("cl_ret1 = %d\n", cl_ret1);return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/382520.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/382520.shtml
英文地址,請注明出處:http://en.pswp.cn/news/382520.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

PyCharm安裝和配置教程

文章目錄官網鏈接錯誤類型&#xff01;1.你的用戶名是中文解決方案PyCharm的漢化和配置PyCharm的簡單使用入門PyCharm和git安裝教程官網鏈接 www.jetbrains.com 錯誤類型&#xff01;1.你的用戶名是中文 安裝第一次各種問題&#xff01;就不多了&#xff0c;反正各種報錯 原…

linux 進程通信 pipe

pipe函數 管道函數 man pipe #include <unistd.h> int pipe(int pipefd[2]);參數介紹&#xff1a;pipefd讀寫文件描述符&#xff0c;0-代表讀&#xff0c; 1-代表寫父子進程實現pipe通信&#xff0c;實現ps aux | grep bash 功能 經常出現的問題&#xff1a; 父進程認為…

軟件工程學習筆記《二》代碼規范

文章目錄軟件工程學習筆記目錄google代碼規范節選python來自google翻譯錯誤注釋的示例命名規范import語句的規范import this 源碼軟件工程學習筆記目錄 [https://blog.csdn.net/csdn_kou/article/details/83754356] google代碼規范 https://github.com/google/styleguide 節…

Linux 進程通信之FIFO

FIFO通信&#xff08;first in first out&#xff09; FIFO 有名管道&#xff0c;實現無血緣關系進程通信。 ----創建一個管道的偽文件 a.mkfifo testfifo 命令創建 b.也可以使用函數int mkfifo(const char *pathname, mode_t mode); ----內核會針對fifo文件開辟一個緩沖區&…

PyCharm和git安裝教程

文章目錄先到官網下載git進入setting&#xff0c;如黃色部分如果你用的是github那么直接setting登陸就行了如果你是gitee的話首先進入setting然后Plugins點擊browse查找gitee如圖所示&#xff01;最后點擊重啟ok《不要自己關閉&#xff0c;否則安裝失敗》安裝好了以后,輸入你的…

linux 進程通信子mmap

mmap 文件–內存映射 函數原型 #include <sys/mman.h>void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);參數介紹&#xff1a; add 傳 NULL length 映射區的長度 protPROT_READ 可讀PROT_WRITE可寫 flagsMAP_SHARED 共享的&#xff0c…

malloc和calloc的區別

是否對申請的區域進行初始化而已 但是我想你也知道我們寫程序的時候多用malloc而很少用calloc&#xff0c;何解&#xff1f; 因為calloc雖然對內存進行了初始化&#xff08;全部初始化為0&#xff09;&#xff0c;但是同樣也要降低效率的 calloc相當于 p malloc(); memset(p,…

linux信號學習02

未決信號集與阻塞信號集(信號屏蔽字) 阻塞信號集&#xff1a; 將某些信號加入集合&#xff0c;對他們設置屏蔽&#xff0c;當屏蔽x信號后&#xff0c;再收到該信號&#xff0c;該信號的處理將推后(解除屏蔽后) 未決信號集&#xff1a; a. 信號產生&#xff0c;未決信號集中描述…

task_struct 結構如何查看及分析

cd /find -name sched.hvim usr/src/kernels/3.10.0862.6.3.el7.x86_64/include/linux/sched.hhttps://www.cnblogs.com/zxc2man/p/6649771.html 進程是處于執行期的程序以及它所管理的資源&#xff08;如打開的文件、掛起的信號、進程狀態、地址空間等等&#xff09;的總稱。…

linux 與信號集操作相關的函數

與信號集操作相關的函數 #include <signal.h> 清空信號集 全都為0 int sigemptyset(sigset_t *set);填充信號集 全都為1 int sigfillset(sigset_t *set);添加某個信號到信號集 int sigaddset(sigset_t *set, int signum);從集合中刪除某個信號 int sigdelset(sigset_t *s…

軟件工程學習筆記《三》代碼優化和性能測試

文章目錄軟件工程學習筆記目錄如何在開源社區提問&#xff1f;代碼審查代碼優化運行結果參數解釋代碼優化原則對常見的數據結構排序算法進行測試關于冒泡排序優化的探討結果軟件工程學習筆記目錄 [https://blog.csdn.net/csdn_kou/article/details/83754356] 如何在開源社區提…

linux信號捕捉

信號捕捉&#xff0c;防止進程意外死亡 signal函數 man signal #include <signal.h> typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler);參數介紹&#xff1b; signum 要捕捉的信號 handler 要執行的捕捉函數指針&#xff0c…

軟件工程學習筆記《目錄》

軟件工程學習筆記《目錄》 軟件工程學習筆記《一》什么是軟件工程 軟件工程學習筆記《二》代碼規范 軟件工程學習筆記《三》代碼優化和性能測試 軟件工程學習筆記《四》需求分析

linux進程利用SIGCHLD信號,來實現父進程回收子進程

子進程執行完畢后&#xff0c;會向父進程發出 SIGCHLD信號 &#xff0c; 這段代碼實現的就是i&#xff0c;父進程接受到子進程 發出的SIGCHLD信號&#xff0c;實現對子進程進行回收&#xff0c;從而避免僵尸進程 #include <stdio.h> #include <unistd.h> #include…

WWW軟件全球使用排名

https://w3techs.com/technologies/overview/web_server/all Apache份額一直下降呀&#xff01;

軟件工程學習筆記《四》需求分析

文章目錄軟件工程學習筆記《目錄》需求工程師當代的需求工程師需要具備的能力當代的需求工程師需要努力的方向當代的需求工程師需要注意的錯誤需求的定義需求目標需求分析的實質需求分析的關鍵應該涵蓋的內容&#xff1f;需求規約&#xff08;作為較客觀的參照&#xff09;單個…

linux守護進程

先了解 linux系統中 會話的概念 會話是進程組的更高一級&#xff0c;多個進程組對應一個會話。 會話是一個或多個進程組的集合 創建一個會話需要注意以下5點事項&#xff1a; a. 調用進程不能是進程組組長&#xff0c; 該進程變成新會話首進程&#xff08;session header&#…

python3爬蟲學習筆記

文章目錄python3的文本處理jieba庫的使用統計hamlet.txt文本中高頻詞的個數統計三國演義任務高頻次數爬蟲爬取百度首頁爬取京東某手機頁面BeautifulSoup使用request進行爬取&#xff0c;在使用 BeautifulSoup進行處理&#xff01;擁有一個更好的排版BeautifulSoup爬取百度首頁原…

linux 線程學習初步01

線程的概念 進程與線程內核實現 通過函數clone實現的 ps -Lf pidLinux內核線程實現原理 同一個進程下的線程&#xff0c;共享該進程的內存區&#xff0c; 但是只有stack區域不共享。 線程共享資源 a.文件描述符表 b.每種信號的處理方式 c.當前工作目錄 d.用戶id和組id 線程…

python3字符串處理,高效切片

高級技巧&#xff1a;切片&#xff0c;迭代&#xff0c;列表&#xff0c;生成器 切片 L [Hello, World, !]print("-------1.一個一個取-------") print(L[0]) print(L[1]) print(L[2])print("-------2.開辟一個新列表把內容存進去-------") r [] for i…