檔案I/O(1)檔案描述符
對核心而言,所以開啟的檔案都通過檔案描述符引用。每個進程都有一些與之關聯的檔案描述符。檔案描述符是一個非負整數。當開啟一個現有檔案或建立一個新檔案時,核心向進程返回一個檔案描述符。當讀或寫一個檔案時,使用open或creat返回的檔案描述符標識該檔案,將其作為參數傳送給read和write。
一般有三個以及開啟的檔案描述符,他們是:
0:標準輸入 STDIN_FILENO
1:標準輸出 STDOUT_FILENO
2標準錯誤輸出 STDERR_FILENO
每行後面的符號常量是依從POSIX而定的。
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);
pathname是要開啟或建立檔案的名字。
flag用來定義開啟檔案所採取的的動作,必須調用以下模式之一
O_RDONLY, O_WRONLY, O_RDWR分別代表唯讀,唯寫,讀寫方式開啟。
open還可以包括以下可選模式的組合
O_APPEND:把寫入資料追加到檔案的尾端
O_CREAT:若檔案不存在,則建立它。使用此選項時,需要第三個參數mode,用其指定該新檔案的存取權限。
O_EXCL:如果同時指定了O_CREAT,而檔案存在,則會出錯。用此可以測試一個檔案是否存在,如果存在,則建立檔案,這使測試和建立兩者成為一個原子操作。
O_TRUNC: 如果此檔案存在,而且為唯寫或讀寫成功開啟,則將其長度截為0。
open返回的檔案描述符一定是最小的未用描述符數值。這一點被某些應用程式用在標準輸入,標準輸出或標準錯誤輸出上。如,一個程式關閉了自己的標準輸出,然後再次調用open,檔案描述符1就會被調用,並且標準輸出將被有效重新導向到另一個檔案或裝置。
POSIX規範還標準化了一個creat調用,此函數等效於
open(pathname,O_WONLY |O_CREAT | O_TRUNC, mode);
close函數
#include <unistd.h>
int close(int fd);
close調用終止一個檔案描述符fd與對應檔案之間的關聯。檔案描述符被釋放後並能重新使用。close調用成功返回0,出錯返回-1.
關閉一個檔案時會釋放該進程加在檔案上的所有記錄鎖。當一個進程終止時,核心自動關閉它所有開啟的檔案。
lseek函數
每個開啟的檔案都有一個與其相關聯的”當前檔案位移量”。按系統預設情況,當開啟一個檔案時,除非指定O_APPEND選項,否則該位移量被設定為0。lseek可以為一個開啟的檔案設定位移量。
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, intwhence);
offset用來指定位置,whence參數定義該位移值的用法。whence可取以下值:
SEEK_SET: The offset is set to offset bytes.
SEEK_CUR: The offset is set to its current locationplus offset bytes.
SEEK_END: The offset is set to the size of the fileplus offset bytes.
成功調用返回從檔案頭到檔案指標被設定處的位元組位移值,失敗返回-1。參數offset定義在<sys/types.h>中。
當位移量大於檔案長度時,出現空洞,空洞不佔用儲存區。
read函數
#include <unistd.h>
ssize_t read(int fd, void *buf, size_tcount);
將與檔案描述符fd關聯的檔案中讀入count個字元放到buf中。返回讀入的位元組數,它可能小於請求的位元組數。如果read調用返回0,就表示未讀入任何資料,已到達了檔案尾。返回-1,就表示出錯。
write函數
#include <unistd.h>
ssize_t write(int fd, const void *buf,size_t count);
把緩衝區buf的前count個位元組寫入與檔案描述符fd相關聯的檔案中。返回實際寫入的位元組數,通常與count值相同;否則表示出錯。出錯的一個常見原因是:磁碟已寫滿,或者超出了一個給定進程的檔案長度限制。
執行個體:建立一個檔案,寫入資料,移動當前位移量,在讀資料。
#include<unistd.h> //<unistd.h>必須最早出現,因為它可能會影響到其他標頭檔。#include<stdio.h>#include<fcntl.h>#include<string.h>#include<errno.h>int main(){ char* filename = ".//file"; char buf[100]; char buf1[5]; int fd; printf("open a file to write\n"); if((fd = open(filename,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH ))==-1) { perror("cannot open file\n"); return 1; } printf("open file successfully!\n"); printf("input a string:");gets(buf);//write intofile if(write(fd,buf,strlen(buf)) !=strlen(buf)) { perror("cannot write intofile\n"); return 1; } close(fd); printf("open file to read.\n"); if((fd=open(filename,O_RDONLY)) == -1) { perror("cannot open thefile.\n"); return 1; } if(lseek(fd,3,SEEK_SET) == -1) { perror("lseek erroe\n"); return 1; } //read from the file if(read(fd,buf1,4)==-1) {perror("read error.\n"); return 1; } printf("read from file is%s\n",buf1); close(fd); return 0;}
執行與輸出結果:
chen123@ubuntu:~$gcc -o io io.c
chen123@ubuntu:~$./io
open a file towrite
open filesuccessfully!
input a string:akxivbaslzkncxcasbxbwwvaidxbd
open file toread.
read from fileis ivba