免费注册 查看新帖 |

Chinaunix

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

about fgets [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-18 09:30 |只看该作者 |倒序浏览

                today i am coding a somall program. fgets will be used in it. But i do not know this function very well, so i man it and write some test code .
char *fgets(char *s, int size, FILE *stream);
I view the function fgets in the manual. In the manual it is said like this:
fgets() reads in at most one less than size characters from stream and stores then 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 '\0' is stored after the last character in the buffer.
there are some occasions need to think about.
1. What happened if the buffer is not enough the store a line .
  line:123456789/n
  mem[5]:\000\000\000\000\000
  if we use the mem to store the line , the result is
   mem[5]:1234\0
  understand ? note the last space is always '\0'.
2.what happened if the buffer is just the size of the line.
  line:123456789\n
  mem[10]:
  the the result is :
  mem[10]:123456789\0
note the last bytes stores '\0', not '\n'.
3. What happened if the buffer is larger than the size of the line
  line: 123456789\n
  mem[15]={0}
the result is :
  mem[15]:123456789\n\000...\000
understand ?
so, this is the fgets .
when you use it next time, i think you will do well with it.
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/50866/showart_1999759.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP