1. 程式人生 > >fgets 之用法

fgets 之用法

今天在看ObjectiveC,看到了一個fgets的例子,才發現對於fgets的理解不夠透徹。

fgets 的使用方法:char *fgets(char *string, int n, FILE *stream) 

從檔案stream中讀取n-1個字元/一行(若一行不滿n-1個),string接收字串

 如果n <= 0,返回NULL

 如果n == 1,返回" ",也就是一個空串

 如果成功,返回值等於string, 也就是獲得字串的首地址

 如果出錯,或者讀到FILE的結尾,返回NULL.

下面看例子:


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

int main( )
{
 FILE* wordFile = fopen("words.txt","r");
 char word[100];
 
 while(fgets(word, 100, wordFile))
 {
  word[strlen(word)-1] = '/0';
  printf("%s is %d characters long/n", word, strlen(word));
 }
 fclose(wordFile);
 return (0);
}

words.txt的內容如下(共4行,第4行,為空):

apple trees
many books on the desk
have a cup of water

執行後,輸出結果如下:

apple trees is 11 characters long

many books on the desk is 22 characters long

have a cup of water is 19 charcters long

從上面的例子看出,若一行不滿100個字元時,fgets可以從檔案中讀取一行。這點,以前,沒有注意到。

fgets 函式的使用

fgets 既可以讀檔案,又可以讀標準輸入,而且可以防止溢位。但是它只能輸入字串(且能讀到回車符/n),故而用scanf語句的較多。scanf語句可以輸入各種格式的資料,其功能較為強大。

fgets 的使用方法:char *fgets(char *string, int n, FILE *stream) 

從檔案stream中讀取n-1個字元/一行(若一行不滿n-1個),string接收字串

 如果n <= 0,返回NULL

 如果n == 1,返回" ",也就是一個空串

 如果成功,返回值等於string, 也就是獲得字串的首地址

 如果出錯,或者讀到FILE的結尾,返回NULL

//通過while迴圈一行行取,讀到檔案末尾就是NULL了 ----讀取整個檔案
#include <stdio.h>

void main( void )
{
FILE *stream;
char line[100];

if( (stream = fopen( "file.txt", "r" )) != NULL )
{
while( fgets( line, 100, stream ) != NULL)
printf( "%s", line);
fclose( stream );
}
}

以下是fgets這個函式的實現:

/***
*fgets.c - get string from a file
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines fgets() - read a string from a file
*
*******************************************************************************/

#include <cruntime.h>
#include <stdio.h>
#include <dbgint.h>
#include <file2.h>
#include <internal.h>
#include <mtdll.h>
#include <tchar.h>

/***
*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:
*
*******************************************************************************/

_TSCHAR * __cdecl _fgetts (
_TSCHAR *string,
int count,
FILE *str
)
{
REG1 FILE *stream;
REG2 _TSCHAR *pointer = string;
_TSCHAR *retval = string;
int ch;

_VALIDATE_RETURN(( string != NULL ) || ( count == 0 ), EINVAL, NULL);
_VALIDATE_RETURN(( count >= 0 ), EINVAL, NULL);
_VALIDATE_RETURN(( str != NULL ), EINVAL, NULL);

if (count == 0)
{
return NULL;
}

/* The C Standard states the input buffer should remain
unchanged if EOF is encountered immediately. Hence we
do not blank out the input buffer here */

/* Init stream pointer */
stream = str;

_lock_str(stream);
__try {

#ifndef _UNICODE
_VALIDATE_STREAM_ANSI_SETRET(stream, EINVAL, retval, NULL);
#endif /* _UNICODE */
if(retval!=NULL)
{
while (--count)
{
if ((ch = _fgettc_nolock(stream)) == _TEOF)
{
if (pointer == string) {
retval=NULL;
goto done;
}

break;
}

if ((*pointer++ = (_TSCHAR)ch) == _T('/n'))
break;
}
*pointer = _T('/0');
}


/* Common return */
done:

; }
__finally {
_unlock_str(stream);
}

return(retval);
}

-----------------------------------------------------------------------------------------------------------------

另: fgetc 和 getchar 是一個一個字元的讀取,