fgets 之二

来源:互联网 发布:淘宝新开店铺没有关注 编辑:程序博客网 时间:2024/06/07 16:29

转一篇关于fgets的文章:

原文来自:http://blog.csdn.net/lly66/archive/2008/10/19/3105779.aspx

 

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 是一个一个字符的读取,

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lly66/archive/2008/10/19/3105779.aspx

原创粉丝点击