Linux C 文件操作函數(~上善止水~)


翻翻筆記,整理一下 C 語言中的文件操作函數 ~~~~~~,多注意細節,maybe 細節決定成敗~

 

1. fopen

/* fopen(打開文件)
 *
 * 相關函數 open,fclose
 *
 * 表頭文件 #include<stdio.h>
 *
 * 函數原型 FILE * fopen(const char * path,const char * mode);
 *
 * 函數說明:
 *          path 字符串包含欲打開的文件路徑及文件名
 *          mode 字符串則代表着流形態
 *           
 * mode 有下列幾種形態字符串:
 *
 * r    打開只讀文件,該文件必須存在。
 *
 * r+   打開可讀寫的文件,該文件必須存在。
 *
 * w    打開只寫文件,若文件存在則文件長度清為0,即該文件內容會消失,
 *      若文件不存在則建立該文件。
 *       
 * w+   打開可讀寫文件,若文件存在則文件長度清為零,即該文件內容會消失,
 *      若文件不存在則建立該文件。
 *
 * a    以附加的方式打開只寫文件。若文件不存在,則會建立該文件,如果文件
 *      存在,寫入的數據會被加到文件尾,即文件原先的內容會被保留。
 *
 * a+   以附加方式打開可讀寫的文件。若文件不存在,則會建立該文件,如果文
 *      件存在,寫入的數據會被加到文件尾后,即文件原先的內容會被保留。
 */

 

上述的形態字符串都可以再加一個b字符,如rb、w+b或ab+等組合,加入b 字符用來告訴函數庫打開的文件為二進制文件,

而非純文字文件。不過在POSIX系統,包含Linux都會忽略該字符。由fopen()所建立的新文件會具

有  S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH(0666)權限,此文件權限也會參考umask值。

返回值

  文件順利打開后,指向該流的文件指針就會被返回。若果文件打開失敗則返回NULL,並把錯誤代碼存在errno 中。

附加說明

  一般而言,打開文件后會作一些文件讀取或寫入的動作,若開文件失敗,接下來的讀寫動作也無法順利進行,

  所以在fopen()后請作錯誤判斷及處理。

實例:

 1 #include<stdio.h>
 2 
 3 int main(int argc, char *argv[])
 4 {
 5     FILE * fp;
 6     
 7     fp=fopen("noexist","a+");
 8     if(NULL == fp) return;
 9     
10     fclose(fp);
11 }

 

2. fprintf 

這里先對經常用的幾個printf系列函數做個了解,

printf: 就是標准輸出,在屏幕上打印出一段字符串來

sprintf:把格式化的數據寫入到某個子符串中,返回值字符串的長度

fprintf: 用於文件操作

/* 功能:傳送格式化輸出到一個文件中/或標准輸出
 *
 * 表頭文件:#include<stdio.h>
 *
 * 函數原型:int fprintf(FILE *stream, char *format[, argument,...]);
 * 
 * 參數:
 *      FILE* 一個FILE型的指針
 *      char* 格式化輸入函數,和printf里的格式一樣
 *
 * 返回值:
 *      成功時返回轉換的字節數,失敗時返回一個負數
 */

實例一:文件操作

#include <stdio.h>
#include <stdlib.h>
#include <error.h>


int main(int argc, char *argv[])
{
    FILE *fp = NULL;
    char str[] = "hello world";
    
    fp = fopen("tt.txt","a+"); // 若文件存現追加在尾部,不存在就會創建tt.txt
    if(NULL == fp) {
       perror("fopen error!"); 
       exit(-1);
    }
        
    fprintf(fp,"%s\n",str);   // 格式化寫入
    fclose(fp);
    
    return 0;
}

實例二:輸出到終端上,這是fprintf 常見的一種操作

static void usage(void)
{
    fprintf(stderr,
            "webbench [option]... URL\n"
            "  -f|--force               Don't wait for reply from server.\n"
            "  -r|--reload              Send reload request - Pragma: no-cache.\n"
            "  -t|--time <sec>          Run benchmark for <sec> seconds. Default 30.\n"
            "  -p|--proxy <server:port> Use proxy server for request.\n"
            "  -c|--clients <n>         Run <n> HTTP clients at once. Default one.\n"
            "  -9|--http09              Use HTTP/0.9 style requests.\n"
            "  -1|--http10              Use HTTP/1.0 protocol.\n"
            "  -2|--http11              Use HTTP/1.1 protocol.\n"
            "  --get                    Use GET request method.\n"
            "  --head                   Use HEAD request method.\n"
            "  --options                Use OPTIONS request method.\n"
            "  --trace                  Use TRACE request method.\n"
            "  -?|-h|--help             This information.\n"
            "  -V|--version             Display program version.\n"
           );
}

插曲stderr 和 stdout 有別 

#include <stdio.h>

extern FILE *stdin;     // 標准輸入
extern FILE *stdout;     // 標准輸出
extern FILE *stderr;    // 標准錯誤

 我們知道,標准輸出和標准錯誤默認都是將信息輸出到終端,那說他們有別,區別在哪里?我們看個題目

問題:下面程序的輸出是什么?

 1 #include <stdio.h>
 2 
 3 
 4 int main()
 5 {
 6     fprintf(stdout, "Hello ");
 7     fprintf(stderr, "World!");
 8     
 9     return 0;
10 }

:輸出結果是 World!Hello,為什么不是Hello World! 呢???

解釋在默認情況下,stdout 是行緩沖的,它的輸出會放在一個buffer里面,只有到換行的時候,才會輸出到屏幕。而 stderr 是無緩沖的,

會直接輸出,舉例來說就是 printf(stdout,"xxx");和 printf(stderr, "xxx\n");前者會憋住,直到遇到新行才一起輸出,而后者不管有么"\n"都輸出。

再看一題:

這 3 句效果不是一樣嘛,有什么區別?

#include <stdio.h>


int main()
{
    fprintf(stderr, "stderr->Hello world!\n");  
    fprintf(stdout, "stdout->Hello world!\n");
    printf("printf->Hello world!\n");
    
    return 0;
}

有區別:

  stdout -- 標准輸出設備(printf("..."))同stdout

  stderr --  標准錯誤輸出設備

兩者默認向屏幕輸出,但如果定向輸出到磁盤文件的時候,就可看出兩者的區別,stdout 輸出到磁盤文件stderr 在屏幕

直接運行:

定向到磁盤文件:

 

3. fscanf 

/*
 * 功能:從一個流中執行格式化輸入
 *
 * 表頭文件:#include<stdio.h>
 *
 * 函數原型:int fscanf(FILE *stream, char *format[,argument...]);
 *
 * 參數解釋:
 *          FILE* 一個FILE型的指針
 *          char* 格式化輸出函數,和scanf里的格式一樣
 * 返回值:
 *          成功時返回轉換的字節數,失敗時返回一個負數
 */
fp = fopen("/local/test.c","a+");
 if(NULL == fp) {  // fopen 后要做錯誤判斷
    perror("fopen error!");
    exit(-1);
 }
 fscanf(fp,"%s",str);

 

4. clearerr

/*
 * 功能:清除文件流的錯誤旗標
 *
 * 相關函數 feof
 *
 * 表頭文件 #include<stdio.h>
 * 函數原型:void clearerr(FILE * stream);
 *
 * 函數說明 clearerr()清除參數stream指定的文件流所使用的錯誤旗標。
 */ 

 

5. fclose

/*
 * 功能:關閉文件
 *
 * 相關函數 close,fflush,fopen,setbuf
 *
 * 表頭文件 #include<stdio.h>
 *
 * 定義函數 int fclose(FILE * stream);
 *
 * 函數說明 
 * fclose()用來關閉先前fopen()打開的文件。此動作會讓緩沖區內的數據寫入文件中,
 * 並釋放系統* 所提供的文件資源。
 *
 * 返回值 
 *      若關文件動作成功則返回0,有錯誤發生時則返回EOF並把錯誤代碼存到errno。
 *
 * 錯誤代碼 
 *      EBADF表示參數stream非已打開的文件。
 * 范例 請參考fopen()
 */

6. fdopen

/*
    功能:將文件描述詞轉為文件指針
    
    相關函數 fopen,open,fclose
    
    表頭文件 #include<stdio.h>
    
    定義函數 FILE * fdopen(int fildes,const char * mode);
    
    函數說明 
    fdopen()會將參數fildes 的文件描述詞,轉換為對應的文件指針后返回。參數mode 字符串則代表着文件指針的流形態,此形態必須和原先文件描述詞讀寫模式相同。
    關於mode 字符串格式請參考fopen()。
    
    返回值 
    轉換成功時返回指向該流的文件指針。失敗則返回NULL,並把錯誤代碼存在errno中。
    
*/
#include<stdio.h>

int main()
{
    FILE * fp =fdopen(0,"w+");
    
    fprintf(fp,"%s/n","hello!");
    fclose(fp);
}

7. feof

/*
    功能:檢查文件流是否讀到了文件尾
    
    相關函數 fopen,fgetc,fgets,fread
    
    表頭文件 #include<stdio.h>
    
    定義函數 int feof(FILE * stream);
    
    函數說明 
            feof()用來偵測是否讀取到了文件尾,尾數stream為fopen()所返回之文件指針。
            如果已到文件尾則返回非零值,其他情況返回0。
    
    返回值 
            返回非零值代表已到達文件尾。
*/ 

8. fflush

/*
    功能:更新緩沖區
    
    相關函數 write,fopen,fclose,setbuf
    
    表頭文件 #include<stdio.h>
    
    定義函數 int fflush(FILE* stream);
    
    函數說明    
            fflush()會強迫將緩沖區內的數據寫回參數stream指定的文件中。如果參數stream為NULL,
            fflush()會將所有打開的文件數據更新。
    返回值 
            成功返回0,失敗返回EOF,錯誤代碼存於errno中。
            
    錯誤代碼 
            EBADF 參數stream 指定的文件未被打開,或打開狀態為只讀。其它錯誤代碼參考write()。
*/

9. fgetc

/*
    功能:由文件中讀取一個字符
    
    相關函數 open,fread,fscanf,getc
    
    表頭文件 include<stdio.h>
    
    定義函數 int fgetc(FILE * stream);
    
    函數說明 
            fgetc()從參數stream所指的文件中讀取一個字符。若讀到文件尾而無數據時便返回EOF。
    返回值 
            getc()會返回讀取到的字符,若返回EOF則表示到了文件尾。
    范例
    復制代碼 代碼如下:
*/
#include<stdio.h>
#include <errno.h>
#include <stdlib.h>


int main()
{
    FILE *fp = NULL;
    int c;
    
    fp=fopen("exist","r");
    if(NULL == fp) {
        perror("fopen error!");
        exit(-1);
    }
    
    while((c=fgetc(fp))!=EOF)
        printf("%c",c);
    
    fclose(fp);
    return 0;
}

10. fgets

/*
    功能:由文件中讀取一字符串
    
    相關函數 open,fread,fscanf,getc
    
    表頭文件 include<stdio.h>
    
    定義函數 char *fgets(char * s,int size,FILE * stream);
    
    函數說明 
        fgets()用來從參數stream所指的文件內讀入字符並存到參數s所指的內存空間,
    直到出現換行字符、讀到文件尾或是已讀了size-1個字符為止,最后會加上NULL作為字符串結束。
    
    返回值 
        gets()若成功則返回s指針,返回NULL則表示有錯誤發生。

*/

#include<stdio.h>
#include <errno.h>
#include <stdlib.h>

int main()
{
    char s[80];
    
    puts(fgets(s,80,stdin),stdout);
    
    return 0;
}
/*執行結果*/
//this is a test /*輸入*/
//this is a test /*輸出*/

11. fileno

/*
    功能:返回文件流所使用的文件描述詞
    
    相關函數 open,fopen
    
    表頭文件 #include<stdio.h>
    
    定義函數 int fileno(FILE * stream);
    
    函數說明 
        fileno()用來取得參數stream指定的文件流所使用的文件描述詞。
        
    返回值 
        返回文件描述詞。
*/

#include<stdio.h>
#include <errno.h>
#include <stdlib.h>

int main()
{
    FILE * fp = NULL;
    int fd;
    
    fp=fopen("/etc/passwd","r");
    if(NULL == fp) {
        perror("fopen error!");
        exit(-1);
    }
    fd=fileno(fp);
    printf("fd=%d/n",fd); // fd = 3
    
    fclose(fp);
    
    return 0;
}

12. fputc

/*
    功能:將一指定字符寫入文件流中
    
    相關函數 fopen,fwrite,fscanf,putc
    
    表頭文件 #include<stdio.h>
    
    定義函數 int fputc(int c,FILE * stream);
    
    函數說明 
        fputc 會將參數c 轉為unsigned char 后寫入參數stream 指定的文件中。
    返回值 
        fputc()會返回寫入成功的字符,即參數c。若返回EOF則代表寫入失敗。
    
*/

#include<stdio.h>
#include <errno.h>
#include <stdlib.h>

int main()
{
    FILE * fp = NULL;
    char a[26]="abcdefghijklmnopqrstuvwxyz";
    int i;
    
    fp= fopen("tmp.txt","w");
    if(NULL == fp) {
        perror("fopen error!");
        exit(-1);
    }
    for(i = 0; i < 26; i++)
        fputc(a[i],fp);
    
    fclose(fp);
    
    return 0;
}

 

13. fputs

/*
    功能:將一指定的字符串寫入文件內
    
    相關函數 fopen,fwrite,fscanf,fputc,putc
    
    表頭文件 #include<stdio.h>
    
    定義函數 int fputs(const char * s,FILE * stream);
    
    函數說明 
        fputs()用來將參數s所指的字符串寫入到參數stream所指的文件內。
        
    返回值 
        若成功則返回寫出的字符個數,返回EOF則表示有錯誤發生。
        
    范例 
        請參考fgets()。
*/

14. fread

/*        
    功能:從文件流讀取數據
    相關函數 fopen,fwrite,fseek,fscanf
    
    表頭文件 #include<stdio.h>
    
    定義函數 size_t fread(void * ptr, size_t size, size_t nmemb, FILE * stream);
    
    函數說明 
        fread()用來從文件流中讀取數據。參數stream為已打開的文件指針,參數ptr
        指向欲存放讀取進來的數據空間,讀取的字符數以參數size*nmemb來決定。
        fread()會返回實際讀取到的nmemb數目,如果此值比參數nmemb來得小,則代表可能
        讀到了文件尾或有錯誤發生,這時必須用feof()或ferror()來決定發生什么情況。
    
    返回值 
        返回實際讀取到的nmemb數目。
*/

#include<stdio.h>
#include <errno.h>
#include <stdlib.h>


int main()
{
    FILE * stream = NULL;char str[10];
    
    stream = fopen("tmp.txt","r");
    if(NULL == stream) {
        perror("fopen error!");
        exit(-1);
    }
    
    fread(str,sizeof(str),1,stream); // 讀 10個字節
    printf("str = [%s]\n", str);
    
    fclose(stream);
    return 0;
}

 

15. freopen

/*
    功能:打開文件
    
    相關函數 fopen,fclose
    
    表頭文件 #include<stdio.h>
    
    定義函數 FILE * freopen(const char * path, const char * mode, FILE * stream);
    
    函數說明 
        path    字符串包含欲打開的文件路徑及文件名,參數mode請參考fopen()說明。
        stream  為已打開的文件指針。freopen()會將原stream所打開的文件流關閉,然后打開參數path的文件。
    返回值     
        文件順利打開后,指向該流的文件指針就會被返回。如果文件打開失敗則返回NULL,並把錯誤代碼存在errno 中。
        
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>


int main()
{
    FILE * fp = NULL;
    
    fp=fopen("/etc/passwd","r");
    if(NULL == fp) {
        perror("fopen error!");
        exit(-1);
    }
    fp=freopen("/etc/group","r",fp);
    
    fclose(fp);
    
    return 0;
}

16. fseek

/*
    功能:移動文件流的讀寫位置
    
    相關函數 rewind,ftell,fgetpos,fsetpos,lseek
    
    表頭文件 #include<stdio.h>
    
    定義函數 int fseek(FILE * stream, long offset, int whence);
    
    函數說明 
        fseek()用來移動文件流的讀寫位置。
        參數:stream為已打開的文件指針,
        參數:offset為根據參數whence來移動讀寫位置的位移數。
        參數:whence為下列其中一種:
            SEEK_SET 從距文件開頭offset位移量為新的讀寫位置。
            SEEK_CUR 以目前的讀寫位置往后增加offset個位移量。
            SEEK_END 將讀寫位置指向文件尾后再增加offset個位移量。
        當whence值為SEEK_CUR 或SEEK_END時,參數offset允許負值的出現。
        
    下列是較特別的使用方式:
    1) 欲將讀寫位置移動到文件開頭時: fseek(FILE *stream,0,SEEK_SET);
    2) 欲將讀寫位置移動到文件尾時:   fseek(FILE *stream,0,0SEEK_END);
    返回值 
        當調用成功時則返回0,若有錯誤則返回-1,errno會存放錯誤代碼。
    附加說明 
        fseek()不像lseek()會返回讀寫位置,因此必須使用ftell()來取得目前讀寫的位置。
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>


int main()
{
    FILE * stream;
    long offset;   
    fpos_t pos; // pos 是一個fpos_t類型的結構體
    
    
    stream=fopen("/etc/passwd","r");
    if(NULL == stream) {
        perror("fopen error!");
        exit(-1);
    }
    fseek(stream,5,SEEK_SET);
    printf("offset=%d\n",ftell(stream));
    
    rewind(stream); // rewind 函數->使文件位置標記指向文件開頭
    fgetpos(stream,&pos);
    printf("offset=%d\n",pos);
    
  // pos = 10; // 編譯器提示:error:incompatible type when assigning to type 'fpos_t'
          //       from type 'int' 
   pos.__pos
= 10; // 正確 給fpos_t 類型的變量賦值 fsetpos(stream,&pos); printf("offset = %d\n",ftell(stream)); fclose(stream); return 0; } /*打印輸出*/ // offset=5 // offset=0 // offset = 10

補充:如何給 fpos_t 類型的變量賦值

我們在上面fseek的實例中用到另外兩個函數 fgetpos fsetpos, 我們看一下兩個函數的原型

/*
 * 頭文件:#include <stdio.h>
 *
 *  函數fgetpos 
 *      是獲取strerm 當前的指針所指的位置並把該指針所指的位置數
 *      存放到pos所值得對象中。pos值以內部格式存儲。
 *   
 *  函數fsetpos 
 *      是設置stream文件的讀寫指針到pos
 *
 *  參數:
 *      stream: 指向文件的文件指針
 *      pos:    fpos_t 類型的結構體
 *
 */
// 函數原型
int fgetpos(FILE *stream, fpos_t *pos);
int fsetpos(FILE *stream, fpos_t *pos);

// fpos_t 結構體
typedef struct {
    __off_t     __pos;
    __mbstate_t __state;
}_G_fpos_t;

所以我們在上面 fseek 實例總,定義了 fpos_t pos; 在給pos賦值的時候,不能直接 pos = 10;(但是網上很多例子都是直接這樣賦值),

正確的賦值應該是:pos.__pos = 10;

 

17. ftell

/*
    功能:取得文件流的讀取位置
    
    相關函數 fseek,rewind,fgetpos,fsetpos
    
    表頭文件 #include <stdio.h>
    
    定義函數 long ftell(FILE * stream);
    
    函數說明 
        ftell()用來取得文件流目前的讀寫位置。參數stream為已打開的文件指針。
        
    返回值 
        當調用成功時則返回目前的讀寫位置,若有錯誤則返回-1,errno會存放錯誤代碼。
        
    錯誤代碼 
        EBADF 參數stream無效或可移動讀寫位置的文件流。
        
    范例 
        參考fseek()。
*/ 

18. fwrite

/*
    功能:將數據寫至文件流
    
    相關函數 fopen,fread,fseek,fscanf
    
    表頭文件 #include <stdio.h>
    
    定義函數 size_t fwrite(const void * ptr,size_t size,size_t nmemb,FILE * stream);
    
    函數說明 
        fwrite()用來將數據寫入文件流中。
        
    參數說明
        stream 為已打開的文件指針,
        ptr    指向欲寫入的數據地址,
        size   總共寫入的字符數以參數size*nmemb來決定。fwrite()會返回實際寫入的nmemb數目。
        
    返回值 
        返回實際寫入的nmemb數目。
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
   
#define nmemb 3

typedef struct test {
    char name[20];
    int size;
}TEST; 

#define set_s(x,y) \
    strcpy(s[x].name,y); \
    s[x].size = strlen(y);


int main()
{
    FILE * stream = NULL;
    TEST s[3];  // 結構體數組
    
    set_s(0, "Linux!");
    set_s(1, "unix!");
    set_s(2, "Window!");
    
    stream = fopen("tmp.txt","w");
    if(NULL == stream) {
        perror("fopen error!");
        exit(-1);
    }
    
    fwrite(s,sizeof(struct test),nmemb,stream);
    
    fclose(stream);
    
    return 0;
}

19. getc

/*
    功能:由文件中讀取一個字符
    
    相關函數 read,fopen,fread,fgetc
    
    表頭文件 #include <stdio.h>
    
    定義函數 int getc(FILE * stream);
    
    函數說明    
        getc()用來從參數stream所指的文件中讀取一個字符。若讀到文件尾而無數據時便返回EOF。
        雖然getc()與fgetc()作用相同,但getc()為宏定義,非真正的函數調用。
        
    返回值 
        getc()會返回讀取到的字符,若返回EOF則表示到了文件尾。
        
    范例 
        參考fgetc()。
*/ 

20. getchar

/*
    功能:由標准輸入設備內讀進一字符
    
    相關函數 fopen,fread,fscanf,getc
    
    表頭文件 #include <stdio.h>
    
    定義函數 int getchar(void);
    
    函數說明 
        getchar()用來從標准輸入設備中讀取一個字符。然后將該字符從unsigned char轉換成int后返回。
        
    返回值 
        getchar()會返回讀取到的字符,若返回EOF則表示有錯誤發生。
        
    附加說明 
        getchar()非真正函數,而是getc(stdin)宏定義。
*/

21. mktemp /mkstemp

/*
    功能:產生唯一的臨時文件名
    
    相關函數 tmpfile
    
    表頭文件 #include <stdlib.h>
    
    定義函數 char * mktemp(char * template);
    
    函數說明 
        mktemp()用來產生唯一的臨時文件名。
        
    參數說明
        template所指的文件名稱字符串中最后六個字符必須是XXXXXX。產生后的文件名會借字符串指針返回。
        
    返回值     
        文件順利打開后,指向該流的文件指針就會被返回。如果文件打開失敗則返回NULL,
        並把錯誤代碼存在errno中。
        
    附加說明 
        參數template所指的文件名稱字符串必須聲明為數組,如:
        char template[ ]="template-XXXXXX";
        不可用char * template="template-XXXXXX";
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
   

int main()
{
    char template[ ]="template-XXXXXX";
    
    mkstemp(template); //使用mktemp時編譯器會提示這個是危險的,用mkstemp
    
    printf("template=%s\n",template);
    
    return 0;
}

運行結果

22. putc

/*
    putc(將一指定字符寫入文件中)
    
    相關函數 fopen,fwrite,fscanf,fputc
    
    表頭文件 #include <stdio.h>
    
    定義函數 int putc(int c,FILE * stream);
    
    函數說明 
        putc()會將參數c轉為unsigned char后寫入參數stream指定的文件中。雖然putc()與fputc()作用相同,
        但putc()為宏定義,非真正的函數調用。
        
    返回值 
        putc()會返回寫入成功的字符,即參數c。若返回EOF則代表寫入失敗。
        
    范例 
        參考fputc()。
*/ 

23. putchar

/*
    putchar(將指定的字符寫到標准輸出設備)
    
    相關函數 fopen,fwrite,fscanf,fputc
    
    表頭文件 #include <stdio.h>
    
    定義函數 int putchar (int c);
    
    函數說明 
        putchar()用來將參數c字符寫到標准輸出設備。
        
    返回值 
        putchar()會返回輸出成功的字符,即參數c。若返回EOF則代表輸出失敗。
        
    附加說明 
        putchar()非真正函數,而是putc(c,stdout)宏定義。
        
    范例 
        參考getchar()。
*/ 

24. rewind

/*
    24.rewind(重設文件流的讀寫位置為文件開頭)
    
    相關函數 fseek,ftell,fgetpos,fsetpos
    
    表頭文件 #include <stdio.h>
    
    定義函數 void rewind(FILE * stream);
    
    函數說明 
        rewind()用來把文件流的讀寫位置移至文件開頭。參數stream為已打開的文件指針。此函數
        相當於調用fseek(stream,0,SEEK_SET)。
        
    
    范例 
        參考fseek()
*/

25. setbuf

/*
    25.setbuf(設置文件流的緩沖區)
    
    相關函數 setbuffer,setlinebuf,setvbuf
    
    表頭文件 #include <stdio.h>
    
    定義函數 void setbuf(FILE * stream,char * buf);
    
    函數說明 
        在打開文件流后,讀取內容之前,調用setbuf()可以用來設置文件流的緩沖區。
        
    參數說明
        stream 為指定的文件流,
        buf    指向自定的緩沖區起始地址。
    如果參數buf為NULL指針,則為無緩沖IO。Setbuf()相當於調用:setvbuf(stream,buf,buf?_IOFBF:_IONBF,BUFSIZ)
    
*/ 

26. setbuffer

/*
    26.setbuffer(設置文件流的緩沖區)
    
    相關函數 setlinebuf,setbuf,setvbuf
    
    表頭文件 #include <stdio.h>
    
    定義函數 void setbuffer(FILE * stream, char * buf,size_t size);
    
    函數說明 
        在打開文件流后,讀取內容之前,調用setbuffer()可用來設置文件流的緩沖區。
        
    參數說明
        tream  為指定的文件流,
        buf    指向自定的緩沖區起始地址,
        size   為緩沖區大小。
        
    stream為指定的文件流,參數buf指向自定的緩沖區起始地址,參數size為緩沖區大小。
    
*/

27. setlinebuf

/*
    27.setlinebuf(設置文件流為線性緩沖區)
    
    相關函數 setbuffer,setbuf,setvbuf
    
    表頭文件 #include <stdio.h>
    定義函數 void setlinebuf(FILE * stream);
    
    函數說明 
        setlinebuf()用來設置文件流以換行為依據的無緩沖IO。
        相當於調用:setvbuf(stream,(char * )NULL,_IOLBF,0);請參考setvbuf()。
    
*/

28. setvbuf

/*
    28.setvbuf(設置文件流的緩沖區)
    
    相關函數 setbuffer,setlinebuf,setbuf
    
    表頭文件 #include <stdio.h>
    
    定義函數 int setvbuf(FILE * stream,char * buf,int mode,size_t size);
    
    函數說明 在打開文件流后,讀取內容之前,調用setvbuf()可以用來設置文件流的緩沖區。
    
    參數說明
        stream  為指定的文件流,
        buf     指向自定的緩沖區起始地址,
        size    為緩沖區大小,
        mode    有下列幾種stream為指定的文件流,
        buf     指向自定的緩沖區起始地址,
        
        mode有下列幾種
            _IONBF 無緩沖IO
            _IOLBF 以換行為依據的無緩沖IO
            _IOFBF 完全無緩沖IO。如果參數buf為NULL指針,則為無緩沖IO。
    

*/

 29. ungetc

/*
    ungetc(將指定字符寫回文件流中)
    
    相關函數 fputc,getchar,getc
    
    表頭文件 #include <stdio.h>
    
    定義函數 int ungetc(int c,FILE * stream);
    
    函數說明 
        ungetc()將參數c字符寫回參數stream所指定的文件流。
        這個寫回的字符會由下一個讀取文件流的函數取得。
        
    返回值 
        成功則返回c 字符,若有錯誤則返回EOF。
*/

30. getline 

/*
    getline 
 
函數原型:man 手冊 ,Linux C 中使用條件

    #define _GNU_SOURCE
    #include <stdio.h>
    
    ssize_t getline(char **lineptr, size_t *n, FILE *stream);
    
函數說明:
    getline 不是C庫函數,而是C++庫函數,它會生成一個包含一串從輸入
    流讀入的字符的字符串,直到一下情況發生會導致生成的字符串結束。
    1) 到文件結束
    2) 遇到函數的定界符
    3) 輸入達到最大限度
參數:
    lineptr 指向存放該行字符串的指針,如果是NULL,則有系統幫助malloc
            請在使用完后free釋放。
                
    n       如果是由系統malloc的指針,請填0
    
    stream  文件指針
   
返回值:
    成功:返回讀取的字符數
    失敗:返回 -1

*/

/*  size_t 與 ssize_t 
 *  
 *  size_t
 *    感謝:
 *      https://blog.csdn.net/lplp90908/article/details/50405899
 *      https://blog.csdn.net/21aspnet/article/details/1540025
 *  
 *      為了增強程序的可移植性,便有了size_t,他是為了方便系統之間的移植而定義的
 *      不同系統上定義的 size_t 可能不一樣。
 *      
 *      32 位系統上,size_t 為 unsigned int ,32位無符號整型
 *      typedef unsigned int size_t;
 *       
 *      64 位系統上,size_t 為 unsigned long , 64位無符號整型
 *      typedef unsigned long size_t;
 *  
 *  ssize_t
 *      
 *      而ssize_t 這個數據類型用來表示可以執行讀寫操作的數據塊的大小,它和
 *      size_t 類似,但必須是signed.它表示的是 signed size_t類型的。
 *
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>


int main()
{
    FILE *fp = NULL;
    char *line = NULL;
    size_t len = 0;
    ssize_t read;   // getline 返回類型
    
    fp = fopen("tmp.txt", "r");
    if(NULL == fp){
        perror("fopen error!");
        exit(-1);
    }
    while(-1 != (read = getline(&line, &len, fp))) {//讀取行信息,'\n'為換行標志
        // %zu 輸出ssize_t 類型變量
        printf("Read line of length %zu :\n", read);
        printf("%s", line);   
    }
    //空間的釋放
    if(line){
        free(line);
    }
    fclose(fp);
    
    return 0;
}

 

后記:青春&熱血,Thank you Ronaldo. 

 

我不是個地道的足球迷,相反我是一個籃球fans,但是我也喜歡C羅,

一起感受一下C羅溫情致敬馬德里的告白~

 


    'Real Madrid have conquered my heart' - Ronaldo's letter to fans

 

These years in Real Madrid, and in this city of Madrid, have been possibly
the happiest of my life.

I only have feelings of enormous gratitude for this club, for its fans and
for this city. I can only thank all of them for the love and affection I have received.

However, I believe that the time has come to open a new stage in my life and that
is why I have asked the club to accept transferring me. I feel that way and I ask
everyone, and especially our fans, to please understand me.

They have been absolutely wonderful for nine years. They have been nine unique years.
It has been an emotional time for me, full of consideration but also hard because
Real Madrid is of the highest demands, but I know very well that I will never forget
that I have enjoyed football here in a unique way.

I have had fabulous team-mates on the field and in the dressing room, I have felt
the warmth of an incredible crowd and together we have won three Championships in
a row and four Championships in five years. And with them also, on an individual
level, I have the satisfaction of having won four Gold Balls and three Gold Boots.
All during my time in this immense and extraordinary club.

Real Madrid has conquered my heart, and that of my family, and that is why more than
ever I want to say thank you: thanks to the club, the President, the directors,
my colleagues, all the technical staff, doctors, physios and incredible workers
that make everything work, that tirelessly pursue every minute detail.

Thank you infinitely once more to our fans and thanks also to Spanish Football.
During these nine exciting years I have played against great players.
My respect and my recognition for all of them.

I have reflected a lot and I know that the time has come for a new cycle. I'm leaving
but this shirt, this badge and the Santiago Bernabeu but they will continue to always
feel part of me wherever I am.

Thanks to all and, of course, as I said that first time in our stadium nine years ago: Go Madrid!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM