2012-03-16 4 views
2

q 키를 눌렀을 때만 멈추고 싶었던 while 루프가 있다면 어떻게 할 것입니까? 그러나, 나는이 루프는C에서 일부 키가 눌러 진 경우에만 중지

을 중지 할 수 있습니다 응답을 맥 OS X의에

모두 getchar가()와 getc 그룹() 일시 정지를 실행할

#define TRUE 1 
#define FALSE 0 
typedef int boolean; 

int main(int argc,char* argv[]){ 
char *script = malloc(MAXPATH); 
script = "ls"; 
boolean a; 
a = TRUE; 
while(a){ //this is the while loop i want to break incase of a keypress 
system(script); 
} 

do something else 
something else.... 

매우 프로그램에 그것을 원하지 않아요

+0

어떤 플랫폼에서? 이 질문은 초록에서 대답 할 수 없습니다. – ibid

+1

어떤 플랫폼입니까? 플랫폼과 관련된 답변이 없습니다. 표준 C 라이브러리에서 제공하는 응답이 없습니다. –

+0

Mac OS X. 10.7.3을 실행 중이다. getchar()과 getc()는 다음과 같다. i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Apple Inc. build 5658 기반) (LLVM 빌드 2336.1.00) – Cripto

답변

1

기본 기능은 다음과 같습니다

getchar() and getc() 

kbhit() is a function returns integer value whenever the key is pressed 

당신은 유닉스 계열 OS에서 선택() 메커니즘을 사용할 수 있습니다 위의 기능

#include <stdio.h> 
#include <conio.h> 
#include <math.h> 
#include <time.h> 

    int main() 
    { 
    int m; 
    clrscr(); 
    do 
    { 

    if(kbhit()) 
    { 
    if((m=getch())==97) 
     { 
     printf("Key a is pressed....\n"); 
     } 
     } 
} while(1); 
    getch(); 
    return 0; 
    } 
+0

통화를 차단합니다. 그리고'kbhit()'는 ANSI C 표준의 일부가 아닙니다. 볼랜드 컴파일러가 제공하는 기능. –

0

메인 루프가 stdin에서 char을 읽는 동안 pthread에서 계산을 수행 할 수 있습니다. SDL 라이브러리는 stdin에서 char을 읽으 려하지 않고 더 나은 입력 컨트롤을 제공합니다. gtk는 또한 윈도우가받는 이벤트를 가지고있다. "xev"명령은 비슷한 방식으로 작동하는 xlib 프로그램입니다. 그것은 빈 창이 열리고 키 이벤트를 읽습니다. 내가 키 누르기를 감지 할 수있는 C에서

2

를 사용할 수 있습니다. 모두 하나 개의 기능에

:

#include <stdio.h> 
#include <stdlib.h> 
#include <sys/time.h> 
#include <sys/types.h> 
#include <sys/select.h> 
#include <unistd.h> 

int main(int argc, char *argv[]) 
{ 
    fd_set readfds; 
    struct timeval tv; 
    int ch; 
    int bool, ret; 

    FD_ZERO(&readfds); 
    bool = 1; 
    while (bool) { 
     FD_SET(STDIN_FILENO, &readfds); 
     tv.tv_sec = 0; 
     tv.tv_usec = 0; 
     /* int select(int nfds, fd_set *readfds, fd_set *writefds, 
     *   fd_set *exceptfds, struct timeval *timeout);   */ 
     ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv); 
     if (ret < 0) { 
      perror("select error"); 
      exit(1); 
     } else if (ret == 0) { 
      /* timeout */ 
     } else if (FD_ISSET(STDIN_FILENO, &readfds)) { 
      ch = fgetc(stdin); 
      if (ch == 'q') { 
       bool = 0; 
      } 
     } 
     sleep(1); 
     fprintf(stderr, "."); 
    } 

    return 0; 
} 
+0

거의 모든 경우 select()에 비해 poll()을 선호한다. 가능한 경우 poll()보다 OSX/Linux에서 kqueue/epoll을 선호합니다. – alexchandel

1

나는 alexchandel의 대답 기뻐요. 내가 설문 조사 들어 본 적이 없었다()

여론 조사() 질문자의 플랫폼과 같은 POSIX 스타일의 시스템을위한 좋은 답변

_kbhit() MS Windows에서 간단한 대답이다. 그들의 설문 조사()는 (100)는

은 man 페이지를 읽어 표준 입력을 위해,

내 파일 핸들은 {0 timout 밀리 초, 1 내 목록에서 한 기술자 블록을 의미

물론 다른 당신이 심문 할 수있는 많은 이벤트들, 나는 단지 POLLIN을 원했을 뿐이었다.

#include <stdio.h> 
#include <poll.h> 
#include <errno.h> 

static struct pollfd attention = { 0, POLLIN } ; 

int main() 
{ 
    int x, y; 

    for (;;) 
    { 
     x = poll(&attention, 1, 100); 

     if (x < 0) 
     { 
     printf("problem %d\n", errno); 
     break; 
     } 
     else if (x) 
     { 
     printf("you rang %x ?", x); 
     y = getc(stdin); 
     printf(" %c of course\n", y); 
     if (y == '.') break; 
     } 
    } 

    return 0; 
} 
관련 문제