文件I/O操作

零、文件操作符

文件操作符是一個非負證書,是一個用於描述被打開文件的索引值

標準輸入stdin、標準輸出stdout和標準出錯stderr的索引值是0、1、2


一、文件的操作

1、文件的創建、打開與關閉

(1)文件的打開

#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)
open函數返回,若成功返回文件描述符,若出錯返回-1
pathname使用絕對路徑或相對路徑

flags是打開文件方式的參數,定義在fcntl.h文件中,O_RDONLY,O_WRONLY,O_RDWR三個參數只能指定其中一個

flags取值 含義
O_RDONLY 以只讀方式打開文件
O_WRONLY 以只寫方式打開文件
O_RDWR 以讀寫方式打開文件
O_CREATE 若打開文件不存在則創建文件。使用此選項需要同時使用第三個參數mode說明該文件的存取許可
O_EXCL 如果同時指定了O_CREATE,而文件已經存在,則導致調用出錯
O_TRUNC 如果文件存在,並且爲只讀或只寫方式打開,則將其長度截斷爲0
O_NOCTTY 如果pathname指的是終端tty,則不將此設備分配作爲此進程的控制終端
O_APPEND 每次寫時都加到文件的尾端
O_NONBLOCK 如果pathname指的是一個FIFO、一個塊特殊文件或一個字符特殊文件,則選擇項爲此文件的本次打開操作和後續的I/O操作設置爲非阻塞方式
O_NONELAY 同O_NONBLOCK
O_SYNC 只在數據被寫入外存或其他設備之後操作才返回

mode是用於指定所創建文件的權限,使用|來組合

mode取值 對應八進制數 含義
S_ISUID 04000 設置用戶識別號
S_ISGID 02000 設置組織別號
S_SVTX 01000 粘帖位
S_IRUSR 00400 文件所有者的讀權限位
S_IWUSR 00200 文件所有者的寫權限位
S_IXUSR 00100 文件所有者的執行權限位
S_IRGRP 00040 所有者同組用戶的讀權限位
S_IWGRP 00020 所有者同組用戶的寫權限位
S_IXGRP 00010 所有者同組用戶的執行權限位
S_IROTH 00004 其他用戶的讀權限位
S_IWOTH 00002 其他用戶的寫權限位
S_IXOTH 00001 其他組用戶的執行權限位
S_IRWXU   S_IRUSR|S_IWUSR|S_IXUSR,文件所有者的讀、寫、執行
S_IRWXG   S_IRGRP|S_IWGRP|S_IXGRP,文件所有者同組用戶的讀、寫、執行
S_IRWXO   S_IROTH|S_IWOTH|S_IXOTH,其他組用戶的讀、寫、執行

(2)creat函數

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat (const char *pathname,mode_t mode)
create函數返回,成功返回只寫方式打開的文件描述符,出錯爲-1

pathname、mode含義與open相同

creat的不足指出是以只寫方式打開所創建的文件,若要讀取該文件,必須先調用create和clos然後在調用open

現在可以用下列方式調用open,達到上面的效果

open(pahtname,O_RDWR|O_CREAT|O_TRUNC,mode)

(3)close函數

#include <unistd.h>
int close(int fd)
close函數返回,成功返回0,出錯-1

2、文件的定位

當前文件位移量,非負整數,度量一個文件開始處計算的字節數,讀寫操作從當前文件位移量處開始

調用lseek函數顯示的定位一個打開文件

#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd,off_t offset,int whence)
fd表示已打開文件的描述符

offset表示位移量的大小,單位字節

whence,解釋參數offset大小,下面是取值和含義

whence取值 含義
SEEK_SET 將該文件的位移量設置爲距文件開始處offset個字節
SEEK_CUR 將該文件的位移置量設置爲其當前增加offset個字節,offset可爲正或負
SEEK_END 將該文件的位移量設置爲文件長度加offset個字節,offset可爲正或負

3、文件的讀寫

(1)、read函數

用read函數打開文件讀取數據

#include <unistd.h>
ssize_t read(int fd,void *buf,size_t count)
返回,讀到的字節數,若已到文件尾端返回0,出錯返回-1

fd表示進行操作的文件描述符

buf是一個指向緩衝區的指針

count表示本次操作將要讀取的數據字節數


(2)、write函數

用write函數向打開文件寫數據

#include <unistd.h>
ssize_t write (int fd,void *buf,size_t count)
返回,成功爲已寫的字節數,出錯爲-1

fd操作的文件描述符

buf指向緩衝區的指針,放入要寫入文件的數據

count表示本次操作寫入文件的數據字節數


三、文件的屬性操作

1、改變文件訪問權限

#include <sys/types.h>
#include <sys/stat.h>
int chmod (const char *pathname,mode_t mode)
int fchmod(int fd,mode_t mode)
chmod,fchmod返回,成功0,出錯-1
chmod對指定文件進行操作,fchmod對已經打開的文件進行操作

mode_t可以用16進制的0777來表示,比shell中的chmod 777多個0


2、改變文件所有者

#include <sys/types.h>
#include <unistd.h>
int chown(const char *pathname,uid_t owner gid_t group)
int fchown(int fd,uid_t owner,gid_t group)
int lchown(const char *pathname,uid_t owner,gid_t group)
返回,成功返回0,出錯爲-1

chown修改文件所有者,pathname絕對路徑或相對路徑,owner文件所有者標識號,group組標識號

fchown修改已打開文件的所有者,fd文件描述符,owner、group相同

lchown針對符號鏈接文件,更改鏈接文件不是鏈接指向的文件


3、重命名

#include <stdio.h>
int rename (const char *oldname,const char *newname)
返回,成功爲0,出錯爲-1

rename會將oldname所指定的文件名修改爲參數newname所指定的文件名

若newname已存在,則會被刪除

oldname和newname指向同一個文件時,直接返回成功


4、修改文件長度

#include <sys/types.h>
#include <unistd.h>
int truncate (const char *pathname,off_t len)
int ftruncate(int fd,off_t len)
返回,成功爲0,出錯爲-1


四、其他操作

1、從stat結構提中取回文件信息

linux系統的所有文件都有個與之對應的索引節點,該節點中包含了文件的相關信息,這些信息被保存在stat結構體中

#include <sys/types.h>
#include <sys/stat.h>
int stat(const char *pathname,struct stat * sbuf)
int fstat(int fd,struct stat *sbuf)
int lstat(const char *pathname,struct stat *sbuf)
返回,成功0,出錯-1

2、複製現存的文件描述符

#include <unistd.h>
int dup(int fd)
int dup2(int fd,int fd2)
返回,成功爲新的文件描述符,出錯爲-1

dup返回當前可用文件描述符最小數值,dup2可以用fd2指定新描述符的數值

3、fcntl改變已經打開文件的性質

#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int fcntl(int fd,int cmd)
int fcntl(int fd,int cmd,long arg)
沒什麼用的函數?!

4、sync和fsync

將修改後保存在緩存中的那日哦那個寫入磁盤

#include <unistd.h>
void sync(void)
int fsync(int fd)
返回,成功0,出錯-1

sync只將修改過得塊緩存排入寫隊列返回,不等帶實際I/O操作結束

fsync指引用單個文件,等待I/O結束,然後返回


五、特殊文件操作

1、目錄文件操作

mkdir,rmdir

#include <sys/types.h>
#include <sys/stat.h>
int mkdir(const char *pathname,mode_t mode)
#include <unistd.h>
int rmdir(const char *pathname)
mkdir創建目錄

rmdir刪除空目錄

opendirclosedir,readdir

#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *pathname)
int closedir(DIR *dp)
struct dirent *readdir (DIR *dp)

opendir打開目錄

closedir關閉目錄

readdir目錄文件的讀取



chdir,fchdir,getcwd

chdir,fchidir更改當前工作目錄

getcwd獲得當前工作目錄絕對路徑

#include <unistd.h>
int chidir(const char *pathname)
int fchdir (int fd)
char *getcwd(char *buf,size_t size)



2、鏈接文件操作

(1)硬鏈接

創建鏈接link

#include <unistd.h>
int link(const char *pathname1,const char *pathname2)
返回,成功0,出錯-1
pathname2存在時返回-1,root可以創建目錄的鏈接

(2)刪除鏈接

#include <unistd.h>
int unlink(const char *pathname)
#include 
int remove(const char *pathname)
返回,成功爲0,出錯爲-1

remove可以刪除鏈接,也可以刪除目錄

(3)符號連接

#include<unistd.h>
int symlink (const *actualpath,const char *sympath)
返回,成功0,出錯-1




3、管道文件操作

管道文件用於不同進程間的數據和信息傳遞,一個進程將要傳遞的數據或信息寫入管道的一段,另一個進程從管道另一端取得所需數據

#include <stdio.h>
int pipe(int filedes[2])
返回,成功返回0,出錯爲-1

參數filedes是含有兩個元素的數組,調用pipe成功創建管道後,將返回兩個文件描述符,分別到管道的兩端


4、設備文件

處理/dev下的文件











發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章