fget是如何工作的? [英] How fget works?

查看:71
本文介绍了fget是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 gcc(Ubuntu 4.8.2-19ubuntu1)4.8.2
我正在编写一个非常简单的脚本,以将字符串作为输入并使用一些自定义消息将其打印出来.第一个用户输入T(获取字符串的次数),然后通过 fgets .
进行输入.我使用了&

I am using gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
I am writing a very simple script to take string as input and print the same with the some custom message. First user enters the T(no. of times to take string) and then takes input by fgets.
. I used this&this as reference.
I am getting a very strange output ie fgets is adding some extra new lines and even loop is not working properly for T=2 it is asking input only once.
Can anybody explain me what is wrong with snippet. Thanks in advance!

#include<stdio.h>
#include<string.h>
int main()
{
  int T;
  scanf("%d",&T);

 while(T--){
    char str[100]={""};
    int i;
    printf("Hello\n");
    fgets(str,80,stdin);
    i = strlen(str)-1;
    if(str[i]=='\n')
    str[i]='\0';
    printf("World\n");
    printf("%s\n",str);

  } 
   return 0;

} 

请参阅T = 2的图像参考,即使T = 2,它仅接收一次字符串,并且打印语句的顺序也不符合预期.

Please see the image reference for T=2, even T=2 it is taking string only once and order of printing statement is also not as expected.

推荐答案

这是因为您的scanf()通话

scanf("%d",&T);

不会消耗输入所伴随的换行符\n.

does not consume the new line character \n accompanied by your input.

输入T时,输入的是 2 Enter .

scanf()使用2,看到\n并停止使用,将\n保留在输入缓冲区中.

scanf() consumes the 2, sees \n and stops consuming, leaving \n in the input buffer.

调用fgets()时,它会在缓冲区中看到\n并将其作为输入.

When the fgets() gets called, it sees the \n in the buffer and treats this as the input.

要解决此问题,您有几种选择(按我的主观偏好的降序排列):

To solve this, you have a few choices (in decreasing order of my subjective preference):

  1. 使用fgets()获取第一个输入,然后使用strtol()进行解析以获取T.

  1. Use fgets() to get the first input, then parse it using strtol() to get T.

scanf()之后添加一个额外的fgets().

Add an extra fgets() after the scanf().

添加getchar()以消耗一个额外的字符.如果您确定在输入之后仅出现一个\n,则此方法有效.因此,例如,如果您键入 2 Space Enter ,则它将不起作用.或者,您可以在scanf()之后使用while(getchar() != '\n');消耗所有内容直到新行,但是如果空行是您以后的fgets()调用的有效输入,则可能会引起问题.

Add getchar() to consume one extra character. This works if you are certain that exactly one \n will be present after the input. So for example it won't work if you type 2SpaceEnter. Alternatively, you may use while(getchar() != '\n'); after the scanf() to consume everything up to a new line, but this may cause problems if an empty line is a valid input to your later fgets() calls.

如果实现支持,则可以使用fpurge(stdin)__fpurge(stdin).

If your implementation supports it, you may use fpurge(stdin) or __fpurge(stdin).

而且,非常重要的是,请不要使用fflush(stdin) 除非,否则您的实现必须明确定义其行为.否则,这是未定义的行为.您可以参考此问题以了解更多详细信息.另外,请注意,如果您的输入可以通过管道传递到程序中,则fpurge()fflush()方法可能无法正常工作.

And, very importantly, do not use fflush(stdin) unless your implementation clearly defines its behavior. Otherwise it is undefined behavior. You may refer to this question for more details. Also, note that the fpurge() and fflush() methods may not work correctly if your input can be piped into the program.

这篇关于fget是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆