C语言 mac osx终端中的信号EOF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21364313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:40:39  来源:igfitidea点击:

Signal EOF in mac osx terminal

ckernighan-and-ritchie

提问by Morpheus

I am stumped by the 1.5.2 question in K&R. I googled for sometime and found out that i have to supply the EOF input after entering the characters.

我被 K&R 中的 1.5.2 问题难住了。我用谷歌搜索了一段时间,发现我必须在输入字符后提供 EOF 输入。

long nc = 0;

while (getchar() != EOF)
    ++nc;
printf("%ld\n", nc);

return 0;

I tried both commnad-D and control-D as EOF inputs but nothing worked. Any Idea how to supply the EOF for mac osx?

我尝试了 commnad-D 和 control-D 作为 EOF 输入,但没有任何效果。知道如何为 mac osx 提供 EOF 吗?

回答by Eric Postpischil

By default, OS X (formerly Mac OS X) terminals recognize EOFwhen Ctrl-Dis pressed at the beginning of a line.

默认情况下,OS X(以前称为 Mac OS X)终端会识别EOF何时在行首按下Ctrl- D

In detail, the actual operation is that, when Ctrl-Dis pressed, all bytes in the terminal's input buffer are sent to the running process using the terminal. At the start of a line, no bytes are in the buffer, so the process is told there are zero bytes available, and this acts as an EOFindicator.

具体来说,实际操作是,当按下Ctrl-时D,终端输入缓冲区中的所有字节都使用终端发送到正在运行的进程。在一行的开头,缓冲区中没有字节,所以进程被告知有零字节可用,这充当了一个EOF指示器。

This procedure doubles as a method of delivering input to the process before the end of a line: The user may type some characters and press Ctrl-D, and the characters will be sent to the process immediately, without the usual wait for enter/return to be pressed. After this “send all buffered bytes immediately” operation is performed, no bytes are left in the buffer. So, when Ctrl-Dis pressed a second time, it is the same as the beginning of a line (no bytes are sent, and the process is given zero bytes), and it acts like an EOF.

此过程兼作在行尾前向进程传递输入的一种方法:用户可以输入一些字符并按Ctrl- D,字符将立即发送到进程,而无需通常等待输入/返回按下。执行此“立即发送所有缓冲的字节”操作后,缓冲区中没有剩余字节。因此,当第二次按下Ctrl-D时,它与一行的开头相同(不发送字节,进程被赋予零字节),它的作用就像一个EOF.

You can learn more about terminal behavior by using the command “man 4 tty” in Terminal. The default line discipline is termios. You can learn more about the termios line discipline by using the command man termios.

您可以通过在终端中使用命令“man 4 tty”了解有关终端行为的更多信息。默认的线路规则是 termios。您可以使用命令了解有关 termios 行规范的更多信息man termios

回答by RADan

If you want to see what EOF is set as in your terminal, you can type

如果您想查看终端中设置的 EOF,您可以输入

stty all

on my mac, this gives the output -

在我的 Mac 上,这给出了输出 -

speed 9600 baud; 24 rows; 80 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
    -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
-extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
-ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
-dtrflow -mdmbuf
discard dsusp   eof     eol     eol2    erase   intr    kill    lnext   
^O      ^Y      ^D      <undef> <undef> ^?      ^C      ^U      ^V      
min     quit    reprint start   status  stop    susp    time    werase  
 1       ^\      ^R      ^Q      ^T      ^S      ^Z      0       ^W     

You can see four lines up from the bottom, three cells in eof is ^D.

你可以看到从底部向上四行,eof 中的三个单元格是 ^D。

There's a fuller description herewhich is where I found the information.

这里有更完整的描述这是我找到信息的地方。

回答by Morpheus

I just figured it out. You have to press Ctrl+D+D. Hold the control down and press Dtwice. I have to say what a weird key sequence.

我只是想通了。你必须按Ctrl+ D+ D。按住控件并按D两次。我不得不说一个多么奇怪的按键顺序。