gets()函数输入回车会怎么样?

测试昵称666 2010-07-15 01:29:22

int n;
scanf("%d", &n); //这里接受一个n后下面的s第一次length = 0,输出是空行.
while(gets(s) != NULL) //返回NULL
{
printf("length: %d\n", strlen(s));
printf("%s\n", s);
}




int n;
//scanf("%d", &n); //去掉这句后下面的gets(s)为何每次的回车就不会接受?
while(gets(s) != NULL) //返回NULL
{
printf("length: %d\n", strlen(s));
printf("%s\n", s);
}



小弟愚钝,求讲解。
...全文
1958 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
测试昵称666 2010-07-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 cattycat 的回复:]
char *gets( char *buffer );
从stdio流中读取字符串,直至接受到换行符或EOF时停止,并将读取的结果存放在str指针所指向的字符数组中。换行符不作为读取串的内容,读取的换行符被转换为null值,并由此来结束字符串。

这里gets用法说的很明显了,以换行符号为结束符的,所以换行符不记在内。第一次scanf数字后,剩下换行符在缓冲中了,所以长度是0.
[/Quote]
好吧,多谢猫哥。
有这句话就行了:
读取的换行符被转换为null值,并由此来结束字符串。

我先就是不明白gets()对换行符是怎么处理的。

还有,结贴率 也不能代表一切吧。有的快的问题我10分钟内就结了。
有的问题我给了100分等到现在还没人回。。。
不过还是谢谢各位好心人。


汗。我还是速度把这个帖子结了吧。给大家一个好印象,呵呵。
zhangweiit 2010-07-15
  • 打赏
  • 举报
回复
char * gets ( char * str );

Get string from stdin
Reads characters from stdin and stores them as a string into str until a newline character ('\n') or the End-of-File is reached.
The ending newline character ('\n') is not included in the string.
A null character ('\0') is automatically appended after the last character copied to str to signal the end of the C string.


Notice that gets does not behave exactly as fgets does with stdin as argument: First, the ending newline character is not included with gets while with fgets it is. And second, gets does not let you specify a limit on how many characters are to be read, so you must be careful with the size of the array pointed by str to avoid buffer overflows.
zhangweiit 2010-07-15
  • 打赏
  • 举报
回复
楼上猫兄说的很明白了,换行符是不会读到gets里面的
zhangweiit 2010-07-15
  • 打赏
  • 举报
回复

char s[20] = "";
int n;
scanf("%d", &n);
fflush(stdin);//加上这一行,就清空换行符了
while(gets(s) != NULL) //返回NULL
{
printf("length: %d\n", strlen(s));
printf("%s\n", s);
}


这段代码主要注意那个fflush
tzcherish 2010-07-15
  • 打赏
  • 举报
回复
来学习的。。。
healer_kx 2010-07-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 cattycat 的回复:]

甘草水平很高,可能看楼主这结贴率不想回答了。
[/Quote]

多谢提醒。。。居然忘记看结帖率了。。。汗。。。果然不像久行江湖之人。。。
cattycat 2010-07-15
  • 打赏
  • 举报
回复
甘草水平很高,可能看楼主这结贴率不想回答了。
cattycat 2010-07-15
  • 打赏
  • 举报
回复
char *gets( char *buffer );
从stdio流中读取字符串,直至接受到换行符或EOF时停止,并将读取的结果存放在str指针所指向的字符数组中。换行符不作为读取串的内容,读取的换行符被转换为null值,并由此来结束字符串。

这里gets用法说的很明显了,以换行符号为结束符的,所以换行符不记在内。第一次scanf数字后,剩下换行符在缓冲中了,所以长度是0.
healer_kx 2010-07-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fuquan2005 的回复:]

楼上的回答好像在骗积分,不过这个问题我也不会,我是进来学习的.
[/Quote]

一语中的,元芳,你是怎么知道的。曾泰,你长进不小哇。我就是在骗分。
tonyzjg 2010-07-15
  • 打赏
  • 举报
回复
你输入一个数字后,按了一个回车,gets(s)就读了那个回车
你在scanf("%d", &n);加一个getchar();
fuquan2005 2010-07-15
  • 打赏
  • 举报
回复
楼上的回答好像在骗积分,不过这个问题我也不会,我是进来学习的.
healer_kx 2010-07-15
  • 打赏
  • 举报
回复
你试一试就知道了,。。。

65,202

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧