標準i/o庫是iso c的標準,在很多作業系統上面都實現。unix檔案i/o函式都是針對檔案描述符的,當開啟乙個檔案的時候,返回該檔案描述符用於後續的i/o操作。而對於標準i/o庫,操作則是圍繞流進行,當用標準i/o庫開啟或者建立乙個檔案時,使得乙個流與檔案相關聯。標準i/o庫使用了緩衝技術,使用緩衝的目的是盡可能減少使用read和write呼叫次數,但是效率不高。每次進行讀寫時候需要複製兩次資料。第一次是在核心和標準i/o緩衝之間(呼叫read和write),第二次是在標準i/o緩衝區和使用者程式中的行緩衝區之間。提供了三種型別的緩衝:全緩衝、行緩衝和不帶緩衝。標準i/o預定義三個檔案指標stdin、stdout和stderr。
當乙個流最初被建立的時候,沒有定向。可以用fwide函式設定流的定向,freopen函式清除乙個流的定向。採用setbuf和setvbuf函式更改緩衝區型別,fflush函式沖洗乙個流。
int fwide(file *stream, int mode); //若流是寬字元定向則返回正值,若是位元組定向則返回負值,如實為定向的則返回0
void setbuf(file *stream, char *buf);
int setvbuf(file *stream, char *buf, int mode, size_t size);
_ionbf unbuffered _iolbf line buffered _iofbf fully buffered
int fflush(file *fp);
i/o操作函式:
file *fopen(const char *path, const char *mode); //開啟乙個指定的檔案
file *fdopen(int fd, const char *mode); //獲取乙個現有的檔案描述符,使得乙個i/o流與該描述符先結合,常用於由建立管道和網路通訊通道函式返回的描述符。
file *freopen(const char *path, const char *mode, file *stream);//在乙個指定的流上開啟乙個指定的檔案
int fclose(file* fp);
一次讀取乙個字元
int fgetc(file *stream);
int getc(file *stream);
int getchar(void);
一次讀取一行
char *fgets(char *s, int size, file *stream);
char *gets(char *s);
一次寫乙個字元
int fputc(int c, file *stream);
int putc(int c, file *stream);
int putchar(int c);
一次寫入一行
int fputs(const char *s, file *stream);
int puts(const char *s);
針對二進位制i/o,一般是結構體型別
size_t fread(void *ptr, size_t size, size_t nmembfile *" stream );
size_t fwrite(const void *ptr, size_t size, size_t nmemb,file *stream);
檔案定位函式
int fseek(file *stream, long offset, int whence); //設定檔案的位置
long ftell(file *stream); //返回當前檔案的位置指示
void rewind(file *stream); //將乙個流的位置設定到檔案的開始位置
int fgetpos(file *stream, fpos_t *pos);
int fsetpos(file *stream, fpos_t *pos);
fgetpos函式將檔案指示器的當前值存入有pos指向的物件中,在以後呼叫fsetpos時,可以使用此值將流重新定位到該位置。
格式化i/o:
#include
int printf(const char *format, ...);
int fprintf(file *stream, const char *format, ...); //寫入到檔案
int sprintf(char *str, const char *format, ...); //格式化字串,可以將其他型別轉換為字串
int snprintf(char *str, size_t size, const char *format, ...);
#include
int vprintf(const char *format, va_list ap);
int vfprintf(file *stream, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
#include
int scanf(const char *format, ...);
int fscanf(file *stream, const char *format, ...); //從檔案中讀取
int sscanf(const char *str, const char *format, ...); //可以提取字串內容,遇到空格停止
#include
int vscanf(const char *format, va_list ap);
int vsscanf(const char *str, const char *format, va_list ap);
int vfscanf(file *stream, const char *format, va_list ap);
在unix系統中,標準i/o庫最終都要呼叫檔案i/o,每個標準i/o流都有乙個與其相關聯的檔案描述符。乙個流可以通過呼叫fileno函式獲取其描述符。
int fileno(file *fp) //在呼叫dup和fcntl函式的時候用到
臨時檔案建立函式tmpnam和tepfile
char *tmpnam(char *
s);file *
tmpfile(void);
針對標準i/o寫個程式進行練習,程式如下:
1 #include 2 #include測試結果如下:3 #include 4 #include 5 6 #define bufsize 1024 7 8 intmain() 9 22 //設定為位元組寬度 23 if(fwide(fp,-1) < 0) 24 printf("byte stream\n"); 25 //設定為行緩衝 26 setvbuf(fp,buf,_iolbf,bufsize); 27 printf("enter file content:\n"); 28 gets(line); 29 printf("write to file.\n"); 30 fputs(line,fp); 31 close(fp); 32 fp = fopen(filename,"r+"); 33 if(fp ==null) 34 38 printf("read from file.\n"); 39 fgets(line,99,fp); 40 printf("%s\n",line); 41 close(fp); 42 return 0; 43 }
unix環境高階程式設計 標準IO
標準io庫 不僅在unix上,在很多作業系統上都實現了標準的io庫,它處理了很多細節,例如緩衝區分配,優化長度執行io等。流和file物件 對於標準的io庫,它們的操作是圍繞流 stream 進行的。當用標準io庫開啟或建立乙個檔案時,已經使乙個流和乙個檔案相關聯,標準的io檔案流可用於單位元組和多...
UNIX 環境高階程式設計 標準IO 未完
函式原型 file fopen const char path,const char mode 函式功能 開啟乙個檔案 函式引數 path 字串包含欲開啟的檔案路徑及檔名 mode 字串則代表著流形態。包含下面的幾種模式 mode有下列幾種形態字串 r 以唯讀方式開啟檔案,該檔案必須存在。r 以可讀...
UNIX環境高階程式設計學習筆記三 標準I O庫的介紹
include include intfwid file fp,int mode 指定流寬頻 void setbuf file restrict fp,char restrict buf 開啟或關閉緩衝機制 void setvbuf file restrict fp,char restrict bu...