[牛人杂谈] fgets 之用法

[复制链接]
 楼主| yiyigirl2014 发表于 2017-1-28 11:07 | 显示全部楼层 |阅读模式
今天在看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可以从文件中读取一行。这点,以前,没有注意到。

 楼主| yiyigirl2014 发表于 2017-1-28 11:08 | 显示全部楼层
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);
}

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

 楼主| yiyigirl2014 发表于 2017-1-28 11:09 | 显示全部楼层
#include <stdio.h>
int fgetc(FILE *stream);
char *fgets(char *s, int size, FILE *stream);
int getc(FILE *stream);
int getchar(void);
char *gets(char *s);

fgetc()读取文件指针stream所指向文件的下一个字符,返回值是所读取字符强制类型转换成整数的值,如果到达文件尾部或者出错,则返回EOF。
getc()与fgetc()函数相同,只是它是一个宏实现。
getchar()等同于getc(stdin)。
gets()从标准输入读取一行到字符串指针s所指向的缓冲区,直到行结束或遇到一个EOF,然后用'\0'替代它。注意:该函数不检查缓冲区是否够大,是否有溢出。
fgets()从文件指针stream所指向的文件中,最多取出size个字符存放到s所指向的换中去中。遇到EOF或一行结束时,读取停止。如果读取一行,它将该行存放到缓冲区,在最后一个字符的后边添加'\0'并放到缓冲区。
返回值:
fgetc(),  getc() 和getchar()成功时返回读取字符的ASCII码值,失败时返回EOF。
       gets() 和fgets() 成功时返回字符串的指针s,失败时返回NULL指针。
 楼主| yiyigirl2014 发表于 2017-1-28 11:11 | 显示全部楼层
  1. #include <stdio.h>

  2. int
  3. main(int argc, char **argv)
  4. {
  5.         int tmp;
  6.         FILE *fp;
  7.         char ptr[10];
  8.         char *p;

  9.         fp = fopen("/work/tmp/c/txt", "r");
  10.         if(fp == NULL){
  11.                 printf("open txt fail\n");       
  12.                 return -1;
  13.         }
  14.        
  15.         /*fgetc function test*/
  16.         if((tmp = fgetc(fp)) != EOF){
  17.                 printf("tmp = %d, the character we get is %c\n", tmp, (char)tmp);
  18.         }

  19.         /*fgets function test*/
  20.         if((p = fgets(ptr, 5, fp)) != NULL){
  21.                 printf("the string we get is %s\n", ptr);       
  22.         }

  23.         /*getc function test*/
  24.         if((tmp = getc(fp)) != EOF){
  25.                 printf("tmp = %d, the character we get is %c\n", tmp, (char)tmp);
  26.         }
  27.        
  28.         /*getchar function test*/
  29.         if((tmp = getchar()) != EOF){
  30.                 printf("tmp = %d, the characeter we get is %c\n", tmp, (char)tmp);       
  31.         }

  32.         return 0;

  33. }


643757107 发表于 2017-1-28 12:36 | 显示全部楼层
单片机的get就是通过串口输入内容
643757107 发表于 2017-1-28 12:44 | 显示全部楼层
不知道要不要配置什么实现通过串口来用这个get函数。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

229

主题

3672

帖子

10

粉丝
快速回复 在线客服 返回列表 返回顶部

229

主题

3672

帖子

10

粉丝
快速回复 在线客服 返回列表 返回顶部