Mor*_*eus 36 c kernighan-and-ritchie
我被K&R的1.5.2问题困扰了.我google了一段时间后发现我必须在输入字符后提供EOF输入.
long nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
return 0;
Run Code Online (Sandbox Code Playgroud)
我尝试将commnad-D和control-D作为EOF输入,但没有任何效果.任何想法如何为mac osx提供EOF?
Eri*_*hil 65
默认情况下,OS X(以前的Mac OS X)终端识别EOF时Ctrl- D在一行的开头被按下.
详细地说,实际操作是,当按下Ctrl- 时D,终端输入缓冲区中的所有字节都将使用终端发送到正在运行的进程.在行的开头,缓冲区中没有字节,因此告知进程有零字节可用,这充当EOF指示符.
此过程兼作在行结束前向进程传递输入的方法:用户可以键入一些字符并按Ctrl- D,并且字符将立即发送到进程,而无需通常等待进入/返回按下.在执行"立即发送所有缓冲的字节"操作之后,缓冲区中不会留下任何字节.因此,当Ctrl- D第二次按- 时,它与行的开头相同(没有发送字节,并且进程为零字节),并且它的作用类似于EOF.
您可以使用终端中的命令"man 4 tty"了解有关终端行为的更多信息.默认的线路规则是termios.您可以使用该命令了解有关termios线路规则的更多信息man termios.
RAD*_*Dan 13
如果要查看终端中设置的EOF,可以键入
stty all
Run Code Online (Sandbox Code Playgroud)
在我的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
Run Code Online (Sandbox Code Playgroud)
您可以从底部看到四行,eof中的三个单元格是^ D.
这里有更全面的描述, 这是我找到信息的地方.