首页 > 其他 > 详细

关于fgets()函数

时间:2020-06-26 17:34:30      阅读:77      评论:0      收藏:0      [点我收藏+]

函数原型:char *fgets(char *s, int size, FILE *stream);

功能描述:fgets()  reads  in  at  most one less than size characters from stream and stores them into the buffer pointed to by s.  Reading stops after  an  EOF or a newline.  If a newline is read, it is stored into the buffer.  A terminating null byte (‘\0‘) is stored after the last character in  the  buffer.

“fgets从流上读取字符数不超过size的一行字符并将其存储到s指向的buffer中,当遇到新的一行或文件结束标志时结束。如果读到一个新行(a newline),会把它存储到buffer中。最后会在最后一个字符的后面添加一个字符串结束标记——‘\0‘。”

以上描述中有个难以理解的地方——什么是一个newline?为什么把它也存入buffer中?

实际上,经过代码测试可知:newline其实指的是换行符‘\n‘

另外一个需要注意的地方是最后面添加的字符串结束标记‘\0‘

由于以上两个特性,buf可以直接用printf打印,并且自带换行符。

测试程序如下,该程序可以结合重定向实现文件的复制。注:fgets获取到的换行符并不会导致文件复制时中间多出空行,相反,这正符合文件复制时的需要

/* cp2.c - cp with gets and printf */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DEBUG

int main()
{
    char buf[BUFSIZ];

    while( fgets(buf, BUFSIZ, stdin) != NULL )
    {
#ifdef DEBUG
        if( buf[strlen(buf)] == \0 )
            puts( "fgets gets a line from stdin or file, appended with ‘0‘" );
        if( buf[strlen(buf) - 1] == \n )
            puts( "fgets gets a line with ‘\\n‘" );
#endif
        printf( "%s", buf );
    }
    exit( 0 );
}

测试用例及结果如下:

lim@ubuntu:~/APUE/chap1$ ./cp2 < cp2.c
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
/* cp2.c - cp with gets and printf */
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
#include <stdio.h>
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
#include <stdlib.h>
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
#include <string.h>
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n

fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
#define DEBUG
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n

fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
int main()
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
{
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
    char buf[BUFSIZ];
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n

fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
    while( fgets(buf, BUFSIZ, stdin) != NULL )
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
    {
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
#ifdef DEBUG
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
        if( buf[strlen(buf)] == \0 )
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
            puts( "fgets gets a line from stdin or file, appended with ‘0‘" );
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
        if( buf[strlen(buf) - 1] == \n )
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
            puts( "fgets gets a line with ‘\\n‘" );
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
#endif
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
        printf( "%s", buf );
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
    }
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
    exit( 0 );
fgets gets a line from stdin or file, appended with 0
fgets gets a line with \n
}
lim@ubuntu:~/APUE/chap1$ 

 

关于fgets()函数

原文:https://www.cnblogs.com/uestcliming666/p/13195463.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!