EOF在Windows命令提示符犯规终止输入流 [英] EOF in windows command prompt doesnt terminate input stream

查看:144
本文介绍了EOF在Windows命令提示符犯规终止输入流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是code

code:

#include <stdio.h>
#define NEWLINE '\n'
#define SPACE ' '

int main(void)
{
int ch;
int count = 0;

while((ch = getchar()) != EOF)
{
    if(ch != NEWLINE  && ch != SPACE)
        count++;
}
printf("There are %d characters input\n" , count);

return 0;
}

问:

1)一切都运行得很好,所以会忽略空格和换行和字符的输入输出数量到屏幕(在这个节目,我只是把逗号,感叹号,数字或任何可打印的特殊符号字符符号一样作为字符太)当我打的EOF模拟是 ^ Z

1.)Everything works just fine , it will ignore spaces and newline and output number of characters input to the screen (in this program i just treat comma , exclamation mark , numbers or any printable special symbol character like ampersand as character too) when i hit the EOF simulation which is ^z.

2),但有一些错误的,当我输入这行的程序。例如我输入这个 ABCDEFG ^ Z ,其中一些字符之前并意味着我输入在同一行 ^ Z 终止程序,并打印出总的字符。相反,该计划将继续要求输入。

2.)But there's something wrong when i input this line to the program.For example i input this abcdefg^z , which mean i input some character before and on the same line as ^z.Instead of terminating the program and print out total characters , the program would continue to ask for input.

3)的EOF终止字符输入只能当我指定 ^ Z 在同一行或通过这样 ^ zabvcjdjsjsj 。为什么是这种情况发生?

3.)The EOF terminating character input only works when i specify ^z on a single line or by doing this ^zabvcjdjsjsj.Why is this happened??

推荐答案

这是真的几乎是每个终端驱动程序。你会使用Linux获得相同的行为。

This is true is almost every terminal driver. You'll get the same behavior using Linux.

您的程序是不实际执行循环直到 \\ n ^ Z 曾在已进入了你一行的末尾。终端驱动程序正在缓冲的输入和它直到发生尚未发送给你的过程

Your program isn't actually executing the loop until \n or ^z has been entered by you at the end of a line. The terminal driver is buffering the input and it hasn't been sent to your process until that occurs.

在一行的末尾,按 ^ Z (或 ^ D 在Linux上)做的的使终端驱动程序发送EOF。它只是使得它刷新缓冲区对您的过程(不带 \\ n )。

At the end of a line, hitting ^z (or ^d on linux) does not cause the terminal driver to send EOF. It only makes it flush the buffer to your process (with no \n).

^ Z 在一行的开始(或 ^ D 在Linux上)是间$ P $由终端作为我要信号EOFPTED

Hitting ^z (or ^d on linux) at the start of a line is interpreted by the terminal as "I want to signal EOF".

如果您添加循环内的下列您可以观察此行为:

You can observe this behavior if you add the following inside your loop:

printf("%d\n",ch);

运行程序:

$ ./test
abc                      <- type "abc" and hit "enter"
97
98
99
10
abc97                    <- type "abc" and hit "^z"
98
99

要更好地理解这一点,你必须认识到,EOF不是字符。 ^ Z 是一个用户命令的为终端本身的。由于终端负责取得用户输入并把它传递给过程,这得到棘手,因此混乱。

To better understand this, you have to realize that EOF is not a character. ^z is a user command for the terminal itself. Because the terminal is responsible for taking user input and passing it to processes, this gets tricky and thus the confusion.

要看到这一点的方法是通过按 ^ V 然后按 ^ Z 输入到你的程序

A way to see this is by hitting ^v then hitting ^z as input to your program

^ V 是另一个终端命令,它告诉终端,嘿,我键入下一个东西 - 不跨preT,作为一个终端的命令;它传递给进程的输入,而不是

^v is another terminal command that tells the terminal, "Hey, the next thing I type - don't interpret that as a terminal command; pass it to the process' input instead"

这篇关于EOF在Windows命令提示符犯规终止输入流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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