簡體   English   中英

C如何使用esc從while循環退出

[英]C How to exit from while loop with esc

我想在控制台中輸入“ 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM