fflush()函数不工作的标准输入 [英] fflush() function not working with stdin

查看:147
本文介绍了fflush()函数不工作的标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个愚蠢的问题抱歉。我有C程序提示用户输入年龄和名字和
然后打印年龄和名字在屏幕上。这是我运动,我从书上看到。

I'm sorry for this silly question. I have C program to prompt user to enter age and name and then print the age and name to the screen. This is my exercise that I read from book.

此程序:

#include <stdio.h>

int main (void) {
   int age;
   char name[20];

   puts("Enter your age:");
   scanf("%d",&age);

   fflush(stdin);

   puts("Enter your name:");
   scanf("%s",name);

   printf("Your age is %d\n",age);
   printf("Your name is %s\n",name);

   return 0;
}

当我输入额外的字符到第一个 scanf()的程序终止,并分配多余的字符到下一个 scanf()的

When I enter extra characters to the first scanf() the program terminates and assign the extra characters to the next scanf()

然后,我改变了code,并添加功能名为 clear_buff(),并使用与fgets clear_buff内的功能()如我所料继续读下去stream.The code工作剩余的字符。

And then I changed the code, and add function named clear_buff() and using the fgets function within the clear_buff() to read the remaining characters on stream.The code work as I expected.

#include <stdio.h>

#define MAXLEN 80

void clear_buff(void);

int main (void) {
  int age;
  char name[20];

  puts("Enter your age:");
  scanf("%d",&age);

  clear_buff();

  puts("Enter your name:");
  scanf("%s",name);

  printf("Your age is %d\n",age);
  printf("Your name is %s\n",name);

  return 0;
}

void clear_buff(void){
   char junk[20];
   fgets(junk,MAXLEN,stdin);
}

我的问题是,为什么 fflush(标准输入)在这个程序不工作?

这本书说, fflush 功能清除在stream.And任何缓冲的数据我知道,
fflush()函数是C标准功能,如果与我的工作/ O流。

The book says that fflush function clear any buffered data on the stream.And I know that fflush() function is the C standard function if working with I/O stream.

推荐答案

fflush 是指与输出或更新流工作,并使用它与输入流是不确定的行为从CP preference C型钢为 fflush

fflush is meant to work with output or update streams and using it with an input stream is undefined behavior, from cppreference C section for fflush:

的行为是不确定如果给定的流是输入类型,或者如果给定流是更新型的,但最后的I / O操作不输出动作。

The behavior is undefined if the given stream is of the input type or if the given stream is of the update type, but the last I/O operation was not an output operation.

未定义行为是行为标准没有规定,这意味着结果的计划是联合国predictable,这可能意味着行为似乎正常或者崩溃或其他不良影响。

undefined behavior is behavior that the standard does not specify and it means that the result of your program is unpredictable, which could mean behavior that seems normal or a crash or other undesirable effects.

CP preference是在C99标准草案的语言一致的 7.19.5.2 fflush函数的它说:

cppreference is consistent with the language in the draft C99 standard section 7.19.5.2 The fflush function which says:

如果流指向一个输出流或更新流,其中
  最近一次操作是没有输入,fflush函数导致任何
  该流未写入的数据被传递到主机环境
  要被写入该文件;否则,行为是不确定的。

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

虽然这不是真的你的问题,好像你已经找到了一个解决方法,问题 C,冲洗标准输入占地面积冲洗的一些适当的方法标准输入

Although this is not really your question and it seems like you have figured out a work-around, the question C, flushing stdin covers some proper ways of flushing stdin.

这篇关于fflush()函数不工作的标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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