在将“esc”键输入控制台后,我想退出。但不幸的是,我不知道如何在不重写整个程序的情况下做到这一点。此时它会在ctrl + D之后退出循环。
char* getUserInput(int bytes)
{
char* buffer = malloc(bytes);
char* line = malloc(bytes);
size_t len = 0;
while (getline(&line, &len, stdin) > 0) //I'd like to add one while condition
//here, that will check if esc was pressed, like &&(_getch()!=27)
//or &&(!strcmp(line, (char)27)
{
strcat(buffer, line);
line = malloc(bytes);
}
buffer[strlen(buffer) - 1] = '\0';
return buffer;
}
请参阅下面的代码。希望它会有所帮助。
#include <stdio.h>
int main()
{
char ch;
do{
ch = getch();
printf("Inputed char: %c\n", ch);
}while(ch != 27);
}