檔案類型 | r讀read w寫write x執行execute | |||
- | 普通檔案 | 文本檔案 cat指令可檢視 二進制檔案 cat本身,Linux可執行檔案 | 文本檔案 cat指令可檢視 二進制檔案 cat本身,Linux可執行檔案 | |
l | 連結檔案 | 硬連結檔案和符号連結檔案;指針實作不同檔案系統之間建立一種連結關系 | ||
c | 字元裝置檔案 | 裝置檔案 | 裝置檔案 | |
b | 塊裝置檔案 | |||
p | 管道檔案 | Linux程序間通信。匿名管道和命名管道 | ||
s | 套接口檔案 | socket不同近算計的程序間的通信(套接字) 流式套接口:TCP套接口(面向連接配接的套接口) 資料報套接口:UDP套接口(無連接配接套接口) 原始套接口:SOCK_RAW | ||
d | 目錄檔案 | 系統進行修改 | 系統進行修改 | |
檔案重定向 | 0标準輸入,預設鍵盤可其他 | 1标準輸出預設螢幕可檔案;2标準出錯,預設螢幕可檔案 | ||
重定向标準輸出 | ||||
command > filename | 把标準輸出重定向到filename檔案中 | ls -l > file | ||
command >> filename | 把标準輸出重定向到filename檔案中,方式是追加在現有内容後面 | |||
command 1 filename | 把标準輸出重定向到一個檔案中 | |||
> myfile | 建立一個長度為0的空檔案 | |||
重定向标準輸入 | ||||
command < filename | 以filename檔案的内容作為command指令的标準輸入 | cat < from > to | ||
command < file1 > file2 | 一file1檔案的内容作為command指令的标準輸入,并以file2檔案作為指令執行結果的标準輸出 | |||
command << delimiter | 從标準素如中讀入,直至遇到delimiter分界符 | |||
重定向标準出錯 | ||||
command 2> filename | 把标準出錯重定向到filename中 | |||
command 2>> filename | 把标準出錯重定向追加到檔案中 | |||
檔案建立打開關閉 | //隻有含‘寫’的不存在的檔案會建立,其他會報錯 //r隻讀;w隻寫;a尾增(附加/寫);文本ASCII //rb讀;wb寫;ab尾增;二進制 //以下讀寫↓ //r+;w+;a+;文本ASCII //rb+;wb+;ab+二進制 | |||
#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> | int open(const char *pathName,int flags) | |||
傳回-1出錯,成功傳回檔案描述符fd | int open(const char *pathName,int flags,mode_t mode) | 位址,檔案打開方式,指定建立檔案權限 | ||
#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> | int creat(const char*pathName,mode_t mode) | creat->close->open | ||
傳回-1出錯 | 等效open(pathName,O_RDWR|O_CREAT|O_TRUNC,mode) | |||
#include<unistd.h> | int close(int fd); | fd需要關閉的檔案的描述符 | ||
成功傳回0,出錯傳回-1 | ||||
檔案定位 | #include<sys/types/h> #include<unistd.h> | off_t lseek(int fd,off_t offset,int whence) | fd檔案描述符;offset偏移量位元組數;whence偏移計算起始位置 | |
出錯-1,否則檔案偏移量 | whence :SEEK_SET檔案開始處,SEEK_CUR目前位置,SEEK_END檔案結尾 | |||
檔案讀寫 | #include<unistd.h> | ssize_t read(int fd,void *buf,size_t count) | fd檔案描述符;buf指針緩沖區;count要讀取的位元組數 | |
出錯-1,讀到檔案尾傳回0 | ||||
#include<unistd.h> | ssize_t write(int fd,void *buf,size_t count) | fd檔案描述符;buf指針緩沖區;count要寫入取的位元組數 | ||
出錯-1,否則傳回成功寫入位元組數 | ||||
改變檔案通路權限 | #include<sys/types.h> #include<sys/stat.h> | int chmod(const chat *pathName,mode_t mode) | ||
出錯-1,成功0 | int fchmod(int fd,mode_t mode) | 對已打開進行操作 | ||
改變檔案所有者 | #include<sys/types/h> #include<unistd.h> | int chown(const char*pathName,uid_t owner,gid_t group) | owner所有者辨別符,group表四組辨別号 | |
int fchown(int fd,uid_t owner,gid_t group) | 已打開 | |||
出錯-1成功0 | int lchown(const char*pathname,uid_t owner ,gid_t group) | 針對符号連結檔案愛你 | ||
重命名 | #include<stdio.h> | int rename(const char*oldname,const char*newname) | newname已存在删除 | |
修改檔案長度 | #include<sys/types/h> #include<unistd.h> | int truncate(const char *pathName,off_t len) | ||
出錯-1成功0 | int truncate(int fd,off_t len) | |||
其他 | #include<sys/types.h> #include<sys/stat.h> | int stat(const char*pathName,strut stat*sbuf) | ||
int fstat(int fd,stuct stat*sbuf) | ||||
成功傳回0,出錯傳回-1 | int lstat(const char*pathName,struct stat*sbuf) | |||
UNIXLinux常見基本系統資料類型 | 類型 說明 | struct stat { | ||
caddr_t 核心位址 | mode_t st_mode; //檔案對應的模式,檔案,目錄等 | |||
clock_t 時鐘滴答計數器(程序時間) | ino_t st_ino; //inode節點号 | |||
comp_t 壓縮的時鐘滴答 | dev_t st_dev; //裝置号碼 | |||
dev_t 裝置号 | dev_t st_rdev; //特殊裝置号碼 | |||
fd_set 檔案描述符集 | nlink_t st_nlink; //檔案的連接配接數 | |||
fpos_t 檔案位置 | uid_t st_uid; //檔案所有者 | |||
gid_t 數值組ID | gid_t st_gid; //檔案所有者對應的組 | |||
ino_t i節點編号 | off_t st_size; //普通檔案,對應的檔案位元組數 | |||
mode_t 檔案類型,檔案建立模式 | time_t st_atime; //檔案最後被通路的時間 | |||
nlink_t 目錄項的連結計數 | time_t st_mtime; //檔案内容最後被修改的時間 | |||
off_t 檔案大小和偏移量 | time_t st_ctime; //檔案狀态改變時間 | |||
pid_t 程序ID和程序組ID | blksize_t st_blksize; //檔案内容對應的塊大小 | |||
ptrdiff_t 兩個指針相減的結果 | blkcnt_t st_blocks; //偉建内容對應的塊數量 | |||
rlim_t 資源限制 | }; | |||
sig_atomic_t 能原子的通路的資料類型 | ||||
sigset_t 信号集 | ||||
size_t 對象,大小,不帶符号 | ||||
ssize_t 傳回位元組計數的函數, 帶符号的 | ||||
time_t 月曆時間的秒計數器 | ||||
uid_t 數值使用者ID | ||||
wchar_t 能表示所有不同的字元碼 | ||||
複制已打開檔案描述符 | #include<unistd.h> | int dup(int fd) | ||
出錯傳回-1 | int dup2(int fd,int fd2) | |||
改變已打開檔案性質 | #include<sys/types.h> #include<unistd.h> #include<fcntl.h> | int fcnt1(int fd,int cmd) | ||
出錯傳回-1 | int fcnt1(int fd,int cmd,long arg) | |||
同步指令 | #include<unistd.h> | void sync(void) | 塊緩存排入寫隊列,系統程序(update)30s條用一次sync。定期重新整理核心塊緩存 | |
成功傳回0,出錯傳回-1 | int fsync(int fd) | 引用單個檔案,可用與資料庫應用軟體,確定修改過的塊立即寫到磁盤上 | ||
目錄檔案操作 | #include<sys/types/h> #include<sys/stat.h> | int mkdir(const char*pathName,mode_t mode) | 建立空目錄(至少設定一個執行許可位) | |
成功傳回0,出錯傳回-1 | ||||
#include<unistd.h> | int rmdir(const char*pathName) | 删除空目錄(必須空才能删除) | ||
成功傳回0,出錯傳回-1 | ||||
#include<sys/types.h> #include<dirent.h> | DIR*opendir(const chat*pathName) | 打開目錄 | ||
#include<sys/types.h> #include<dirent.h> | int closedir(DIR*dp) | 關閉目錄 | ||
#include<sys/types.h> #include<dirent.h> | struct dirent*readdir(DIR*dp) | 目錄讀取 | ||
#include<unistd.h> | int chdir(const char*NewpathName) | |||
成功傳回0,出錯傳回-1 | int fchdir(int Newfd) | 改變目前工作路徑 | ||
#include<unistd.h> | char *getcwd(char *buf,size_t size) | 擷取目前工作路徑,緩存長度size | ||
連結檔案操作 | #include<unistd.h> | int link(const char*pathName1,const char*pathName2) | 建立新目錄項pathName2(必須不存在),引用現存檔案pathName2 | 硬連結(同一檔案系統,超級使用者 |
#include<unistd.h> | int unlink(const char*pathName) | 删除目錄項 | ||
#include<unistd.h> | int symlink(const char*actualPath,const char*symPath) | 建立一個指向actual的新目錄項sym | 符号連結(可不同檔案系統,任意使用者 | |
#include<unistd.h> | readlink(const char*pathName,char*buf,int bufSzie) | 檢視pathname連結,資訊存在buf中,并傳回bufsize | ||
管道檔案操作 | #include<stdio.h> | int pipe(int fdes[2]) | 用于不同程序資料資訊傳遞(管道創立成功傳回檔案描述符 | |
普通裝置相當于Linux目錄/dev中的檔案,用基于檔案描述符I/O操作進行讀寫 | ||||
特殊裝置特殊性(錄音帶隻能順序讀寫通路,不能使用lseek随機讀寫 | ||||
設定緩存屬性 | #include<stdio.h> | void setbuf(FILE*fp,char*buf) | 激活或禁止緩沖區開關,BUFSIZ(全緩存)或無緩存 | |
_IOFBF 全緩存 | void setbuffer(FILE*fp,char*buf,size_t size) | 自行指定緩沖區大小 | ||
_IOLBF 行緩存 | void setlinebuf(FILE*fp) | 行緩存 | ||
_IONBF 無緩存 | int setvbuf(FILE*fp,char*buf,int mode,size_t size) | 靈活性 | ||
緩存清洗 | #include<stdio.h> | int fflush(FILE*fp) | 将緩沖區中尚未寫入檔案的資料強制儲存到檔案。成功0,失敗EOF | |
#include<stdio_ext.h> | void fpurge(FILE*fp) | 清除緩沖區資料 | ||
流打開 | #include<stdio.h> | FILE*fopen(const char*pathName,const char*type) | 從頭開始讀寫 | 1. //隻有含w的不存在的檔案會建立,其他會報錯 |
FILE*freopen(const char*pathName,const char*type,FILE*fp) | freopen("input1.txt","r",stdin); freopen("output.txt","w",stdout); | 2. //r隻讀;w隻寫;a尾增;文本ASCII | ||
成功傳回檔案指針,失敗NULL | FILE*fdopen(int fd,const char *type) | 從尾開始讀寫 | 3. //rb讀;wb寫;ab尾增;二進制 | |
int fclose(FILE*fp) | 成功0,失敗EOF;網絡環境檢查必要性 | 4. //以下讀寫↓ | ||
字元輸入 | #include<stdio.h> | int getc ( FILE * fp ); | 讀入字元 int ferror ( FILE *fp)出錯為真 int feof( FILE * fp );檔案結束為真 void cleareer(FILE*fp)清除兩個标志 int ungetc(int c ,FILE*fp)字元送回流中 | 5. //r+;w+;a+;文本ASCII |
int fgetc ( FILE * fp ); | 6. //rb+;wb+;ab+二進制 | |||
int getchar (void);=getc(stdin) | ||||
字元輸出 | #include<stdio.h> | int putc ( int c,FILE * fp ); | ||
int fputc (int c, FILE * fp); | ||||
成功c,出錯EOF | int putchar (int c);=putc(c,stdout) | |||
行輸入 | #include<stdio.h> | char*fgets(char*buf,int n,FILE*fp) | 緩沖區讀到'\n'結束 | |
成功傳回首位址,失敗NULL | char*gets(char*buf); | 緩沖區由使用者提供,緩沖區越界危險 | ||
行輸出 | #include<stdio.h> | int fputs(const char *buf,FILE*restrict fp) | ||
成功傳回位元組數,失敗-1 | int puts(const char*str) | 标準輸出 | ||
直接I/O操作 | #include<stdio.h> | size_t fread ( void * ptr, size_t size, size_t count, FILE * fp ); | ||
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE*fp ); | ||||
格式化輸出 | #include<stdio.h> | int printf ( const char * format, ... ) | ||
int fprintf ( FILE * fp, const char * format, ... ) | 右到左(可以int到char/string) | |||
int sprintf ( char*str, const char * format, ... ) | ||||
int snprintf ( const char *str,size_t size, format, ... ) | ||||
格式化輸入 | #include<stdio.h> | int scanf ( const char * format, ...); | ||
int fscanf ( FILE*fp,char * str, const char * format, ...); | 左到右(可以char/string到int) | |||
int sscanf ( char * str, const char * format, ...); |