五、文件IO函数
對于傳統的操作系統來說,普通的 I/O 操作一般會被內核緩存,這種 I/O 被稱作緩存I/O。本文所介紹的文件訪問機制不經過操作系統內核的緩存,數據直接在磁盤和應用程序地址空間進行傳輸,所以該文件訪問的機制稱作為直接 I/O。Linux 中就提供了這樣一種文件訪問機制,對于那種將 I/O 緩存存放在用戶地址空間的應用程序來說,直接 I/O 是一種非常高效的手段。
一、打開文件函數open
- int open(const char *path, int oflags);
- int open(const char *path, int oflags,mode_t mode);
? ? ?– 參數path表示:路徑名或者文件名。路徑名為絕對路徑名。
? ? ?– 參數oflags表示:打開文件所采取的動作?O_RDONLY文件只讀;O_WRONLY文件只寫;O_RDWR文件可讀可寫;
? ? ? ? O_NOCTTY如果路徑指向終端,則不將設備作為此進程的控制終端 ;O_NDELAY非阻塞方式操作文件。
? ? ?– mode表示:設置創建文件的權限。權限的宏定義很麻煩,可以直接用數字(777)替代。
? ? ?– 返回值:出錯返回-1;否則返回文件句柄。
我們在home/linuxsystemcode/io_file目錄下創建open.c文件,具體代碼如下:
#include <stdio.h>#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>main(){int fd;char *leds = "/dev/leds"; // 存在的文件 char *test1 = "/bin/test1"; // 不存在的文件char *test2 = "/bin/test2"; // 不存在的文件,我們以O_CREAT的形式打開if((fd = open(leds,O_RDWR|O_NOCTTY|O_NDELAY))<0){printf("open %s failed!\n",leds);}printf("\n%s fd is %d\n",leds,fd);if((fd = open(test1,O_RDWR,0777))<0){printf("open %s failed!\n",test1);}printf("%s fd is %d\n",test1,fd); if((fd = open(test2,O_RDWR|O_CREAT,0777))<0){printf("open %s failed!\n",test2);}printf("%s fd is %d\n",test2,fd); }?然后使用GCC編譯器編譯:
最后拷貝出open文件,放到U盤,然后運行,具體如下圖:?
[ 5636.146944] LEDS_CTL DEBUG:Device Opened Success!
[ 5636.151300] LEDS_CTL DEBUG:Device Closed Success!? ? ? ? ? ? 為內核提示的打開和關閉文件成功。
?/dev/leds fd is 3? ? ? ? ? ? ? leds的文件句柄是3
open /bin/test1 failed!? ? ? test1打開失敗
/bin/test1 fd is -1? ? ? ? ? ? ?test1失敗,返回-1;
/bin/test2 fd is 4? ? ? ? ? ? ? test2創建并打開成功,文件句柄為4;
二、創建文件函數create和open
- int creat(const char * pathname, mode_t mode);//注意,這個沒寫錯,就是creat
? ? ? ? ?– 參數path表示:路徑名或者文件名。路徑名為絕對路徑名。
? ? ? ? ?– 參數oflags表示:打開文件所采取的動作:?O_RDONLY文件只讀;O_WRONLY文件只寫;O_RDWR文件可讀可寫
我們在home/linuxsystemcode/io_file目錄下創建creat.c文件,具體代碼如下:?
//標準輸入輸出頭文件 #include <stdio.h>//文件操作函數頭文件 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>main() {int fd;//開發板中已經存在/dev/leds文件char *leds = "/dev/leds";//開發板中不存在的文件/bin/test1char *test1 = "/bin/test1";//開發板中不存在的文件/bin/test2char *test2 = "/bin/test2";//需要新建的文件/bin/test3char *test3 = "/bin/test3";//使用open函數打開文件if((fd = open(leds, O_RDWR|O_NOCTTY|O_NDELAY))<0){printf("open %s failed\n",leds); }printf("%s fd is %d\n",leds,fd);//使用open函數打開不存在的文件,不添加O_CREAT標識符,會報錯if((fd = open(test1, O_RDWR))<0){printf("open %s failed\n",test1); }//打開文件創建文件,添加標志位O_CREAT表示不存在這個文件則創建文件if((fd = open(test2, O_RDWR|O_CREAT,0777))<0){printf("open %s failed\n",test2); }printf("%s fd is %d\n",test2,fd);fd = creat(test3,0777);if(fd = -1){printf("%s fd is %d\n",test3,fd);}else{printf("create %s is succeed\n",test3);} }和上面openy一樣編譯,然后靠到U盤里,插入開發板運行:
?
三、寫函數write
- ssize_t? write(int fd, const void *buf, size_t count);
? ? ? ? ?– 參數fd表示:使用open 函數打開文件之后返回的句柄。
? ? ? ? ?– 參數*buf表示:寫入的數據
? ? ? ? ?– 參數count表示:最多寫入字節數
? ? ? ? ?– 返回值:出錯-1,;其它數值表示實際寫入的字節數
具體程序如下:
//標準輸入輸出頭文件 #include <stdio.h>//文件操作函數頭文件 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h>main() {int fd;char *testwrite = "/bin/testwrite";ssize_t length_w;char buffer_write[] = "Hello Write Function!";if((fd = open(testwrite, O_RDWR|O_CREAT,0777))<0){printf("open %s failed\n",testwrite); }//將buffer寫入fd文件length_w = write(fd,buffer_write,strlen(buffer_write));if(length_w == -1){perror("write");}else{printf("Write Function OK!\n");}close(fd); }運行效果如下:?
?
四、文件的讀取函數read
- ssize_t? read(int fd,void *buf,size_t len);
? ? ? ?– 參數fd:使用open 函數打開文件之后返回的句柄
? ? ? ?– 參數*buf:讀出的數據保存的位置
? ? ? ?– 參數len:每次最多讀len 個字節
? ? ? ?– 返回值:錯誤返回-1,執行成功返回實際讀取值
具體程序如下:
//標準輸入輸出頭文件 #include <stdio.h>//文件操作函數頭文件 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h>#define MAX_SIZE 1000main(){int fd;ssize_t length_w,length_r = MAX_SIZE,ret;char *testwrite = "/bin/testwrite";char buffer_write[] = "Hello Write Function!";char buffer_read[MAX_SIZE];if((fd = open(testwrite,O_RDWR|O_CREAT,0777))<0){printf("open %s failed!\n",testwrite);}length_w = write(fd,buffer_write,strlen(buffer_write));if(length_w == -1){perror("write");}else{printf("Write Function OK!\n");}close(fd);if((fd = open(testwrite,O_RDWR|O_CREAT,0777))<0){printf("open %s failed!\n",testwrite);}if(ret = read(fd,buffer_read,length_r)){perror("read");}printf("Files Content is %s \n",buffer_read);close(fd); }?運行效果如下:
五、關閉函數close
- int close(int fd);
? ? ? ?– 參數fd:使用open 函數打開文件之后返回的句柄。
參考上面write程序最后一行。
最后,我把我們實驗過程的源碼和生成的文件打包一起,需要可以下載(沒積分留言,我發給你):
?
?
總結
- 上一篇: 四、linux基础知识
- 下一篇: 六、字符设备控制