fgets 函數用法


   來說一說fgets(..)函數。

    原型  char *  fgets(char * s, int n,FILE *stream);

    參數:

         s: 字符型指針,指向存儲讀入數據的緩沖區的地址。

         n: 從流中讀入n-1個字符

         stream : 指向讀取的流。

   返回值:

          1. 當n<=0 時返回NULL,即空指針。

          2. 當n=1 時,返回空串"".

          3. 如果讀入成功,則返回緩沖區的地址。

          4. 如果讀入錯誤或遇到文件結尾(EOF),則返回NULL.

          看看這個函數的官方說明:

                       /*** 
                    *char *fgets(string, count, stream) - input string from a stream 
                    * 
                    *Purpose:  
                    * get a string, up to count-1 chars or '\n', whichever comes first, 
                    * append '\0' and put the whole thing into string. the '\n' IS included 
                    * in the string. if count<=1 no input is requested. if EOF is found 
                    * immediately, return NULL. if EOF found after chars read, let EOF 
                    * finish the string as '\n' would. 
                    * 
                    *Entry: 
                    * char *string - pointer to place to store string 
                    * int count - max characters to place at string (include \0) 
                    * FILE *stream - stream to read from 
                    * 
                    *Exit: 
                    * returns string with text read from file in it. 
                    * if count <= 0 return NULL 
                    * if count == 1 put null string in string 
                    * returns NULL if error or end-of-file found immediately 
                    * 
                    *Exceptions: 
                    * 
                    *******************************************************************************/ 

            標准庫中fgets(...)的實現:

             /****************************************************

              char *fgets(char *s, int n,  FILE *stream)

               {

                    register int c;

                    register char *cs;

                    cs=s;

                    while(--n>0 &&(c = getc(stream))!=EOF)

                         if ((*cs++=  c) =='\n')

                           break;

                     *cs ='\0';

                     return (c == EOF && cs == s) ?NULL :s ;

                   }

            /********************************************************

             在用fgets(..)讀入數據時,先定義一個字符數組或字符指針,如果定義了字符指針 ,那么一定要初始化。

        example:

              char s[100]; //可以。

              char *s;  //不可以,因為只是聲明了一個指針。但並沒有為它分配內存緩沖區。

              所以,如果要用指針,則  char *s=(char *)malloc(100*sizeof(char)); 為其分配內存空間,c++中用char *s=new char [100];      如果為分配內存空間,編譯時不會檢查出問題,但運行時會出現未知錯誤。。

        fgets(...)讀入文本行時的兩種情況。

          1。    如果n大於一行的字符串長度,那么當讀到字符串末尾的換行符時,fgets(..)會返回。並且在s的最后插入字符串結束標志'\0'。 而s緩沖區剩余的位置不會再填充。

           example:

              123abc

              fgets(s,10,fp);

              此時,讀入七個字符,123abc\n,實際上還有最后的'\0',所以,strlen(s)=7; 如果要去除末尾的\n,s[strlen(s)-1]='\0';便可。

          2.     如果n小於等於一行的字符串的長度,那么讀入n-1個字符,此時並沒有讀入\n因為並沒有到行尾 ,同樣在最后會插入'\0'.

          example:

            123abc

            char  s[5];

            fgets(s,5,fp);

            這時讀入4個字符,123a,並沒有換行符,所以strlen(s)=4.

       fgets(...)讀入整個文件內容

          通常用while()循環來使fges()讀入文本全部內容,並按行讀入。

           char s[1024];

            while((fgets(s,1024,fp))!=NULL)

             {

                   printf(s);

             }

         當然如果n小於每行的字符個數,也可以讀,只不過讀的次數要多。

          假設一行為 : 123456789

           char s[2];

           int  num=0;

           while((fgets(s,2,fp))!=NULL)

             {

                  printf(s);

                  n++;

             }

            每次讀入一個字符, 最后也會讀完一行,num=10,讀了十次,所以,fgets若沒遇到換行符,會接着從前一次的位置繼續讀入n-1個字符,只要是文本流沒關閉。

         讀入空行的情況:

            第一行   abcdef123

            第二行                       

            第三行  helloworld

            其中第二行為空,fget(..)會把第二行也讀入,因為並未到文件結尾。

            有時我們並不需要空行,可以這樣做。

            while((fgets(s,n,fp))!=NULL)

               {

                    if(strlen(s)!=1)    //注意這兒是1不是0,因為盡管是空行,它也會讀入換行符,strlen(s)=1;

                        printf(s);

               }

         fgets(...)從標准設備讀數據。

            用fgets(...)還也讀入標准輸入設備(一般為鍵盤)的信息

            原型  :  fgets(s,n,stdin);

            假設在控制台下,我們可以用fgets(...)替代gets(),讀入鍵盤輸入的信息,fgets()是安全的,因為不會像gets()有溢出的可能。。

            比如 :輸入 abc

            fgets(s,n,stdin)也會讀入n-1個字符。但是只是從stdin流讀入。。。

            


免責聲明!

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



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