亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1947 | 回復: 3
打印 上一主題 下一主題

[C] fgets函數(shù) [復制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2012-10-28 00:10 |只看該作者 |倒序瀏覽
1.執(zhí)行如下代碼
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.修改代碼后執(zhí)行(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的第二個參數(shù)為81時為什么結(jié)果與預期不一致,我的理解是fgets的第二個參數(shù)是限定從流中輸入的字符個數(shù),只要該值大于我所需要輸入的字符個數(shù)就行了,那為什么太過大了又不行呢?

論壇徽章:
2
程序設(shè)計版塊每日發(fā)帖之星
日期:2015-06-17 22:20:00每日論壇發(fā)貼之星
日期:2015-06-17 22:20:00
2 [報告]
發(fā)表于 2012-10-28 09:20 |只看該作者
提示: 作者被禁止或刪除 內(nèi)容自動屏蔽

論壇徽章:
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 [報告]
發(fā)表于 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 [報告]
發(fā)表于 2012-10-29 20:36 |只看該作者
回復 2# pmerofc


    原來回車符\n也讀進去了,知道了,謝謝!
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP