什么fflush(标准输入)并用C编程? [英] What does fflush(stdin) do in C programing?

查看:144
本文介绍了什么fflush(标准输入)并用C编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C编程很新,我试图了解如何 fflush(标准输入)确实有效。

在下面的例子中确实 fflush(标准输入)清除所有的缓冲区或它清除第三个项目后,什么都进入?我的意思是用户输入帐号,空间,域名,空间,平衡。是真正的,从这个角度上,什么都进入用户将与刷新fflush(标准输入)?和标准输入将不能为空。

为什么我说,是因为它进入while循环,将数据写入文本文件。

我的第二个问题是键Crtl-Z 将告诉OS不再问用户进入输入?

 的printf(请输入帐户名和平衡(用空格隔开)\\ n);
  的printf(请输入EOF结束输入(键Crtl-Z)\\ n);
  的printf(?);
  scanf函数(%D%S%LF,&安培;帐户,名称和放大器;平衡);
  fflush(标准输入);  //写账户,姓名和平衡与fprintf中的文件
  而(!的feof(标准输入))
  {
     // fflush(标准输入);
     fprintf中(cfPtr,%D%S%.2f \\ n,账号,姓名,余额);
     的printf(?);
     scanf函数(%D%S%LF,&安培;帐户,名称和放大器;平衡);
  }  FCLOSE(cfPtr);


解决方案

这个问题的答案是, fflush(流)是输出流只有正式确定,所以 fflush(标准输出)是确定的,但 fflush(标准输入)不是

fflush(流)的目的是让操作系统刷新所有缓冲区的基础文件。对于合法使用的例子,学生们经常有问题,像我的提示不会出现!如果他们这样做:

 的printf(请输入一个数字:);

然而,他们发现这工作得很好:

 的printf(请输入一个数字:\\ n);

当然,他们不想换行后,他们的提示,让他们有一个有点问题。

这样做的原因是输出到标准输出由操作系统缓存,默认行为是(通常)只有实际输出写入到终端时,换行遇到。添加 fflush(标准输出)的printf()解决了这个问题:

 的printf(请输入一个数字:);
fflush(标准输出);

现在,通过类比的工作,人们通常认为 fflush(标准输入)应该丢弃任何未使用的输入,但如果你仔细想想有点不使太大的意义。什么意思刷新的输入缓冲区?它在哪里刷新的的?如果刷新输出缓冲器,输出发送到底层的文件或终端,它最终会风,无论如何,但如果会的输入的最终结束了呢?有没有办法知道!应的行为是什么,如果输入流数据来自一个文件或一个管或一个插座?它不是的所有的明确的输入流什么的行为 fflush()是应该的,但它是在所有情况下的输出流非常清晰。因此, fflush()仅对输出流定义。

为什么错误使用 fflush的(标准输入)成为司空见惯的原因是,很多年前,一些操作系统的没有的实施计划在那里担任过许多人的预期,丢弃未使用的输入。的微软DOS 的是一个很好的例子。出人意料的是,现代版本的的Linux 的也实现 fflush()输入流。

正确的事情做额外无用端子输入简直就是读它,什么也不做它。这几乎与调用 fflush(标准输入),作品随处可见,不依赖于正式未定义行为一样容易。

C 的标准说:


  

如果流点到输出流或更新,其中最近一次操作是没有输入流,fflush函数使该流的任何未写入的数据被传递到主机环境中写入文件; ,否则,这种行为是未定义


POSIX说(也明确委托给的 C 的标准):


  

如果流点到输出流或更新,其中最近一次操作是没有输入流,fflush()须安排任何不成文的数据,该流写入到文件。 ..


的Linux手册页说:


  

有关输出流,fflush()强制通过流的底层写功能给定输出或更新流中的所有用户空间缓冲数据的写入。 对于输入流,fflush()丢弃已经从底层文件获取任何缓冲的数据,但还没有被应用程序所消耗。流的打开状态不受影响。


I am very new in C programing and I am trying to understand how fflush(stdin) really works.

In the following example does fflush(stdin) clears all the buffer or it clears what ever entered after third item? what I mean is user enters account number , space, name , space , balance .Is that true that from this point on, what ever user enters will be flushed with fflush(stdin) ? and stdin won't be empty.

why do I say that is because it enters into while loop and start writing to the text file.

My second question is whether Crtl-Z will tell the OS to stop asking user for entering input?

printf( "Enter the account name and balance. (separated by spaces)\n" );
  printf( "Enter EOF to end input. (Crtl-Z)\n" );
  printf( "? " );
  scanf( "%d%s%lf", &account, name, &balance );
  fflush(stdin);

  // write account, name and balance into file with fprintf
  while ( !feof( stdin ) )
  { 
     //fflush(stdin);
     fprintf( cfPtr, "%d %s %.2f\n", account, name, balance );
     printf( "? " );
     scanf( "%d%s%lf", &account, name, &balance );
  }

  fclose( cfPtr );

解决方案

The answer to this is that fflush(stream) is only formally defined for output streams, so fflush(stdout) is OK, but fflush(stdin) is not.

The purpose of fflush(stream) is to make the operating system flush any buffers to the underlying file. For an example of a legitimate use, students often have problems like "my prompt doesn't appear!" if they do something like:

printf("Enter a number: ");

However, they find that this works just fine:

printf("Enter a number:\n");

Of course, they don't want a newline after their prompt, so they have a bit of a problem.

The reason for this is that the output to stdout is buffered by the OS and the default behavior is (often) only to actually write the output to the terminal when a newline is encountered. Adding an fflush(stdout) after the printf() solves the problem:

printf("Enter a number: ");
fflush(stdout);

Now, working by analogy, people often think that fflush(stdin) should discard any unused input, but if you think about it a little bit that doesn't make much sense. What does it mean to "flush" an input buffer? Where is it "flushed" to? If you flush an output buffer, the output is sent to the underlying file or the terminal, where it would eventually wind up anyway, but where would input "eventually end up anyway"? There's no way of knowing! What should the behavior be if the input stream data comes from a file or a pipe or a socket? It isn't at all clear for input streams what the behavior of fflush() should be, but it's very clear for output streams in all cases. Hence, fflush() is only defined for output streams.

The reason why the erroneous use of fflush(stdin) became commonplace is that, many years ago, a few operating systems did implement a scheme where it worked as many people expected, discarding unused input. Microsoft DOS is a good example. Surprisingly, modern versions of Linux also implement fflush() for input streams.

The right thing to do with "extra" unwanted terminal input is simply to read it and do nothing with it. This is almost as easy as calling fflush(stdin), works everywhere, and doesn't rely on formally undefined behavior.

The C standard says:

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.

POSIX says (also explicitly defers to C standard):

If stream points to an output stream or an update stream in which the most recent operation was not input, fflush() shall cause any unwritten data for that stream to be written to the file, ...

But the Linux manpage says:

For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application. The open status of the stream is unaffected.

这篇关于什么fflush(标准输入)并用C编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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