OSX 10.6.8,GCC 4.2 86_64
#include <stdio.h>
/* count lines in input */
main()
{
int c, nl;
nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}
Run Code Online (Sandbox Code Playgroud)
跑
./a.out
Run Code Online (Sandbox Code Playgroud)
按ctrl+d发送EOF
0D
Run Code Online (Sandbox Code Playgroud)
它应该只是0.为什么它附加D?这是什么意思?
Jon*_*ler 16
我见过这个 - 它也让我很困惑.
终端回显^D,然后0从程序输出,覆盖插入符号.
您可以通过将程序中的打印格式更改为来证明这一点"\n%d\n".
当问'为什么?'时,我去探索.答案是在tty设置中.对于我的终端,输出来自stty -a:
speed 9600 baud; 65 rows; 120 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
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
eol2 = <undef>; erase = ^?; intr = ^C; kill = ^X; lnext = ^V;
min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
stop = ^S; susp = ^Z; time = 0; werase = ^W;
Run Code Online (Sandbox Code Playgroud)
注意echoctl第二行的末尾 - 它是'回声控制字符'.
$ stty -echoctl
$ cat > /dev/null
asdsadasd
$ stty echoctl
$ cat > /dev/null
asasada^D
$
Run Code Online (Sandbox Code Playgroud)
您无法看到它,但是对于每个cat命令,我Control-D在asd字符行的末尾键入a ,在单击返回后键入第二个.提示删除了第二^D个例子中回应的第二个.
因此,如果您不喜欢回显控制字符,请关闭回显:
stty -echoctl
Run Code Online (Sandbox Code Playgroud)
外壳也可能妨碍; 我试验过Control-R,我的shell(bash)决定进入
(reverse-i-search)`': aasadasdadadasdadadadadadsad
Run Code Online (Sandbox Code Playgroud)
我输入了'asd'字符的非原始序列,然后键入Control-R,这就是我最终进入shell的地方.我打断了; 我不确定反向搜索是什么,但我怀疑它是Emacs-ish; 这不是我的预期.
| 归档时间: |
|
| 查看次数: |
582 次 |
| 最近记录: |