C - CTRL +字符的输入测试
-
14-11-2019 - |
题
是想知道如何如何测试在Char数组中保存的输入,如...
char input[INPUT_SIZE];
.
和使用
fgets(input,INPUT_SIZE,stdin);
.
要从用户获取输入,但想知道如何使用IF语句来测试用户输入是否已进行测试
ctrl + d 或任何 ctrl +任何keke?
我用那里使用这样的ascci值..是测试Ctrl D 的示例
if(result = 'EOT') {printf("EOT");}
.
结果是Anwell。
解决方案
You can only test Ctrl+d as your read returning EOF
, see the manual of your read to have more info on this, but generally it returns 0. Same goes for Ctrl+c, as both are sending signals to your program.
For other Ctrl+key combinations, it highly depends on your system.
On linux Ctrl+a and Ctrl+e in a shell or emacs will move you to the beginning or the end / beginning of the line respectively.
The easiest to get what you want is to write a small program using read, unbuffered (see ioctl), with a 8-bytes buffer, and dump your read bytes each time you exit the read.
int nbr;
int i;
char buf[8];
nbr = 42;
while (nbr > 0)
{
nbr = read(0, buf, 8);
i = 0;
while (i < nbr)
printf("%x ", buf[i++]);
printf("\n");
}
You will have the hex version of the ctrl+key received sequences. Likely to begin with \ESC or \033 (the escape character sequence). For example the arrow-up key looks like \033[A