您的位置:首页 > 编程语言 > C语言/C++

C语言文件操作之fgets()

2015-01-23 00:15 239 查看
来说一说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流读入。。。

-==================================================================================

今天在看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可以从文件中读取一行。这点,以前,没有注意到。

原文来自: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 是一个一个字符的读取,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: