为什么EOF字符不工作,如果放在一行的结尾? [英] Why doesn't the EOF character work if put at the end of a line?

查看:319
本文介绍了为什么EOF字符不工作,如果放在一行的结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++,并试图理解为什么EOF字符(Ctrl + Z在Windows上)不会打破while循环,如果放在一行的结尾。

I'm learning C++ and trying to understand why the EOF character (Ctrl + Z on Windows) doesn't break the while loop if put at the end of a line.

我的代码:

int main() {
    char ch;
    while(cin >> ch) {
        cout << ch;
    }
}



当输入^ Z时,循环断开。但是当我输入12 ^ Z,它不。为什么?

When I enter ^Z, the loop breaks. But when I enter 12^Z, it doesn't. Why?

推荐答案

C和C ++标准允许文本流在文本模式 ,这是默认值。这些Unholy事件包括内部换行符标记和外部换行控制字符之间的转换,以及将某些字符或字符序列视为文件结束。在Unix-land中没有这样做,但是在Windows-land中,它已经完成,所以代码只能与原始的Unix-land约定相关。

The C and C++ standards allow text streams to do quite Unholy things in text mode, which is the default. These Unholy Things include translation between internal newline markers and external newline control characters, as well as treating certain characters or character sequences as denoting end of file. In Unix-land it's not done, but in Windows-land it's done, so the the code can relate only to the original Unix-land conventions.

Windows,没有办法编写一个可移植的C或C ++程序,将其输入完全复制到其输入。

This means that in Windows, there is no way to write a portable C or C++ program that will copy its input exactly to its input.

在Unix-land中,这是没有问题的。

While in Unix-land, that's no problem at all.

在Windows中,由单个[Ctrl Z]组成的行按照惯例是文件结束标记。这不仅是在控制台,而且在文本文件(取决于工具上的一点)。 Windows继承了这个从DOS,它反过来继承了CP / M的一般想法。

In Windows, a line consisting of a single [Ctrl Z] is by convention an End Of File marker. This is so not only in the console, but also in text files (depending a bit on the tools). Windows inherited this from DOS, which in turn inherited the general idea from CP/M.

我不知道CP / M从哪里得到它,但它只有<

I'm not sure where CP/M got it from, but it's only similar, not at all the same!, as Unix' [Ctrl D].

在Unix-land中结束文件的一般约定只是没有更多的数据。在控制台中,[Ctrl D]将默认将您键入的文本立即发送到等待程序。当你还没有输入任何东西的时候,0字节被发送,并且返回0字节的读取按照惯例遇到文件结束。

Over in Unix-land the general convention for end of file is just "no more data". In the console a [Ctrl D] will by default send your typed text immediately to the waiting program. When you haven't typed anything on the line yet, 0 bytes are sent, and a read that returns 0 bytes has by convention encountered end-of-file.

主要的区别在于,在Windows中内部文件结束标记是 data ,这可能发生在一个文件内,而在UNIX内部它是缺少数据发生在文件内。当然Windows也支持普通的文件结束(没有更多的数据!)的文本。这使事情复杂化– Windows更复杂。

The main difference is that internally in Windows the text end of file marker is data, that can occur within a file, while internally in Unix it's lack of data, which can't occur within a file. Of course Windows also supports ordinary end of file (no more data!) for text. Which complicates things – Windows is just more complicated.

#include <iostream>
using namespace std;

int main()
{
    char ch;
    while(cin >> ch) {
        cout << 0+ch << " '" << ch << "'" << endl;
    }
}

这篇关于为什么EOF字符不工作,如果放在一行的结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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