免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1919 | 回复: 3
打印 上一主题 下一主题

[C] fgets函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-10-28 00:10 |只看该作者 |倒序浏览
1.执行如下代码
test.c :
     1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #define  MAX 81
      5 int main(void)
      6 {
      7     char name[MAX];
      8     char *ptr;
      9
     10     printf("Hi,what's your name?\n");
     11     ptr=fgets(name,MAX,stdin);
     12     printf("%s? AH! %s!\n",name,ptr);
     13
     14     return 0;

[root@redhat code_C]#gcc test.c -o test.out
[root@redhat code_C]#./test.out
Hi,what's your name?
yzh
yzh
? AH! yzh
!


2.修改代码后执行(MAX改为4):
test.c :
     1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #define  MAX 4
      5 int main(void)
      6 {
      7     char name[MAX];
      8     char *ptr;
      9
     10     printf("Hi,what's your name?\n");
     11     ptr=fgets(name,MAX,stdin);
     12     printf("%s? AH! %s!\n",name,ptr);
     13
     14     return 0;

[root@redhat code_C]#gcc test.c -o test.out
[root@redhat code_C]#./test.out
Hi,what's your name?
yzh
yzh? AH! yzh!
[root@redhat code_C]#


请问1中fgets的第二个参数为81时为什么结果与预期不一致,我的理解是fgets的第二个参数是限定从流中输入的字符个数,只要该值大于我所需要输入的字符个数就行了,那为什么太过大了又不行呢?

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
2 [报告]
发表于 2012-10-28 09:20 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
3 [报告]
发表于 2012-10-28 22:55 |只看该作者

  1. NAME
  2.        fgets - get a string from a stream

  3. SYNOPSIS
  4.        #include <stdio.h>

  5.        char *fgets(char *restrict s, int n, FILE *restrict stream);

  6. DESCRIPTION
  7.        The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a <newline> is read  and  trans-
  8.        ferred to s, or an end-of-file condition is encountered. The string is then terminated with a null byte.

  9.        The  fgets()  function may mark the st_atime field of the file associated with stream for update. The st_atime field shall be marked for update
  10.        by the first successful execution of fgetc(), fgets(), fgetwc(), fgetws(), fread(), fscanf(),  getc(),  getchar(),  gets(),  or  scanf()  using
  11.        stream that returns data not supplied by a prior call to ungetc() or ungetwc().

  12. RETURN VALUE
  13.        Upon  successful completion, fgets() shall return s. If the stream is at end-of-file, the end-of-file indicator for the stream shall be set and
  14.        fgets() shall return a null pointer.  If a read error occurs, the error indicator for the stream shall be set,  fgets()  shall  return  a  null
  15.        pointer,  and shall set errno to indicate the error.
复制代码

论坛徽章:
0
4 [报告]
发表于 2012-10-29 20:36 |只看该作者
回复 2# pmerofc


    原来回车符\n也读进去了,知道了,谢谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP