ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

关于fgets()函数

2020-06-26 16:51:34  阅读:241  来源: 互联网

标签:appended 函数 stdin 关于 file fgets line gets


函数原型: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$ 

 

标签:appended,函数,stdin,关于,file,fgets,line,gets
来源: https://www.cnblogs.com/uestcliming666/p/13195463.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有