在同一行中包含 Ctrl+Z 后,getchar() 继续接受输入
getchar() continues to accept input after including Ctrl+Z in same line
接受和打印字符的简单 c 程序。
int c;
while((c=getchar())!=EOF)
{
putchar(c);
}
我不明白为什么当我在行尾按 Ctrl+Z 时它接受输入
ex 你好(按 Ctrl+Z)
你好(一些符号)
但在离开一行然后按 Ctrl+Z 后它可以正常工作。
我正在使用 Window 7
#include <stdio.h>
int main()
{
int c;
while((c=getchar())!=26)
{
putchar(c);
}
}
您可以使用 CTRL-Z
的 ASCII
值。现在按 CTRL-Z
后将不再接受输入。
你还没有说你正在使用什么系统,[U|Li]nix 或 Windows。这个答案是 Windows 具体的。对于 [Li|U]nix,将对 ctrl-z 的引用替换为 ctrl-d。
在使用终端时,Ctrl-z 不会产生 EOF (-1)(有关详细原因,请参阅 Haccks 和 JeremyP 的良好答案),因此循环不会按照您编写的方式退出。但是,您可以在 while 循环条件中测试 ctrl-z 以退出...
int main ()
{
int c=0;
puts ("Enter text. ctrl-z to exit:");
while(c != 26) //(26 is the ASCII value for ctrl-z)
{
putchar(c);
c = getchar();
}
return 0;
}
顺便说一句,here is a table showing the values for ASCII control characters.
我在 wiki 上找到了答案:
In Microsoft's DOS and Windows (and in CP/M and many DEC operating systems), reading from the terminal will never produce an EOF
. Instead, programs recognize that the source is a terminal (or other "character device") and interpret a given reserved character or sequence as an end-of-file indicator; most commonly this is an ASCII Control-Z, code 26.
当您调用 getchar()
时,它最终会进行 read()
系统调用。 read()
将阻塞,直到它有一些可用的字符。终端驱动程序仅在您按下 return 键或表示输入结束的键时才使字符可用。至少看起来是这样。实际上,它更复杂。
假设 ctrl-Z 意味着任何击键组合意味着 "end of input",原因是 read()
系统调用的工作方式。 ctrl-D(在大多数 Unix 上是 ctrl-D)字符并不表示 "end of input",它表示 "send the current pending input to the application"。
如果您在按 ctrl-D 之前输入了一些内容,该输入将发送到可能在 read()
系统调用中被阻止的应用程序。 read()
用输入填充缓冲区,return 放入缓冲区的字节数。
当你在没有任何输入等待的情况下按下 ctrl-D 时(即你做的最后一件事是点击 return
或 ctrl-D,同样的事情发生但没有字符,所以 read()
returns 0。read()
returning 0 是输入结束的约定。当 getchar()
看到这个时,它 returns EOF
到调用程序。
Stack Exchange 中的这个答案说得更清楚一些
getchar() fution 读取单个字符按 Ctrl+Z 发送 TSTP 信号到您的进程,意味着终止进程 (unix/linux)
接受和打印字符的简单 c 程序。
int c;
while((c=getchar())!=EOF)
{
putchar(c);
}
我不明白为什么当我在行尾按 Ctrl+Z 时它接受输入 ex 你好(按 Ctrl+Z) 你好(一些符号) 但在离开一行然后按 Ctrl+Z 后它可以正常工作。 我正在使用 Window 7
#include <stdio.h>
int main()
{
int c;
while((c=getchar())!=26)
{
putchar(c);
}
}
您可以使用 CTRL-Z
的 ASCII
值。现在按 CTRL-Z
后将不再接受输入。
你还没有说你正在使用什么系统,[U|Li]nix 或 Windows。这个答案是 Windows 具体的。对于 [Li|U]nix,将对 ctrl-z 的引用替换为 ctrl-d。
在使用终端时,Ctrl-z 不会产生 EOF (-1)(有关详细原因,请参阅 Haccks 和 JeremyP 的良好答案),因此循环不会按照您编写的方式退出。但是,您可以在 while 循环条件中测试 ctrl-z 以退出...
int main ()
{
int c=0;
puts ("Enter text. ctrl-z to exit:");
while(c != 26) //(26 is the ASCII value for ctrl-z)
{
putchar(c);
c = getchar();
}
return 0;
}
顺便说一句,here is a table showing the values for ASCII control characters.
我在 wiki 上找到了答案:
In Microsoft's DOS and Windows (and in CP/M and many DEC operating systems), reading from the terminal will never produce an
EOF
. Instead, programs recognize that the source is a terminal (or other "character device") and interpret a given reserved character or sequence as an end-of-file indicator; most commonly this is an ASCII Control-Z, code 26.
当您调用 getchar()
时,它最终会进行 read()
系统调用。 read()
将阻塞,直到它有一些可用的字符。终端驱动程序仅在您按下 return 键或表示输入结束的键时才使字符可用。至少看起来是这样。实际上,它更复杂。
假设 ctrl-Z 意味着任何击键组合意味着 "end of input",原因是 read()
系统调用的工作方式。 ctrl-D(在大多数 Unix 上是 ctrl-D)字符并不表示 "end of input",它表示 "send the current pending input to the application"。
如果您在按 ctrl-D 之前输入了一些内容,该输入将发送到可能在 read()
系统调用中被阻止的应用程序。 read()
用输入填充缓冲区,return 放入缓冲区的字节数。
当你在没有任何输入等待的情况下按下 ctrl-D 时(即你做的最后一件事是点击 return
或 ctrl-D,同样的事情发生但没有字符,所以 read()
returns 0。read()
returning 0 是输入结束的约定。当 getchar()
看到这个时,它 returns EOF
到调用程序。
Stack Exchange 中的这个答案说得更清楚一些
getchar() fution 读取单个字符按 Ctrl+Z 发送 TSTP 信号到您的进程,意味着终止进程 (unix/linux)