为什么需要两个EOF作为输入? [英] Why two EOF needed as input?

查看:300
本文介绍了为什么需要两个EOF作为输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行下面的代码,我使用三个输入(在Ubuntu终端):


  1. abc(Ctrl + D) + D)

  2. abc(Ctrl + D)(Enter Ctrl + D)

  3. li>

代码在所有情况下都能很好地反应。我的问题是:为什么在1)和2)我需要两个EOF?

  #include< iostream& 

int main()
{
int character;

while((character = std :: cin.get())!= EOF){}

std :: cout< std :: endl<字符<< std :: endl;

}


解决方案

EOF字符工作(在规范模式输入,这是默认值)。它实际上从未发送到应用程序,因此更准确地称为 EOF 信号。



EOF 字符(通常为 Ctrl-D )会使当前行立即返回到应用程序。这与 EOL 字符( Enter )的行为非常类似,但与 EOL EOF 字符不包括在该行中。



如果 code>字符在行的开头处键入,然后将零字节返回到应用程序(因为 EOF 字符未发送)。但是如果读取系统调用返回0字节,那么这被认为是文件结束指示。因此,在一行的开头, EOF 将被视为终止输入;



有关更多详细信息,请参阅.Posix

When I run the code below, I use three inputs (in Ubuntu terminal):

  1. abc(Ctrl+D)(Ctrl+D)
  2. abc(Ctrl+D)(Enter)(Ctrl+D)
  3. abc(Enter)(Ctrl+D)

The code reacts well in all cases. My question is: why in 1) and 2) I need two EOF?

#include <iostream>

int main()
{
  int character;

  while((character=std::cin.get())!=EOF){}

  std::cout << std::endl << character << std::endl;

}

That's how the "EOF" character works (in "canonical" mode input, which is the default). It's actually never sent to the application, so it would be more accurate to call it the EOF signal.

The EOF character (normally Ctrl-D) causes the current line to be returned to the application program immediately. That's very similar to the behaviour of the EOL character (Enter), but unlike EOL, the EOF character is not included in the line.

If the EOF character is typed at the beginning of a line, then zero bytes are returned to the application program (since the EOF character is not sent). But if a read system call returns 0 bytes, that is considered an end-of-file indication. So at the beginning of a line, an EOF will be treated as terminating input; anywhere else, it will merely terminate the line and so you need two of them to terminate input.

For more details, see the .Posix terminal interface specification.

这篇关于为什么需要两个EOF作为输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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