2012-03-16 60 views
2

如果我有一个while循环,只有在按下q键时才会停止,我该怎么做。 不过,我不希望它很只有在按下某个键时才停止C

#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.... 

这将在Mac OS X上运行

两者的getchar()和GETC()停顿,这使得循环停止

响应程序
+0

在哪个平台上?这个问题不能在摘要中回答。 – ibid 2012-03-16 04:10:08

+1

哪个平台?没有不是平台特定的答案。标准C库没有提供答案。 – 2012-03-16 04:10:38

+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 build 2336.1.00) – Cripto 2012-03-16 04:12:22

回答

1

本地函数与我可以检测一个按键是:

getchar() and getc() 

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

可以使用上述功能

#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标准的一部分。它只是Borland编译器提供的一个功能 – 2012-03-16 04:54:13

0

当主循环读取stdin上的字符时,可以使计算在pthread中运行。如果您不仅想从stdin读取char,则SDL库具有更好的输入控件。 gtk也有窗口收到的事件。命令“xev”是一个适用于linux的xlib程序,其工作原理类似。它会打开一个空白窗口,并在关键事件发生时读取。用C

2

可以使用select()的机制在类似Unix的操作系统。

所有功能于一体:

#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

几乎在所有情况下都优先选择poll()。如果可能的话,更喜欢使用poll()的OSX/Linux上的kqueue/epoll。 – alexchandel 2016-02-04 21:05:51

1

我很高兴alexchandel的回答。 ()

民意调查()我从来没有听说过投票是POSIX风格的系统中,如提问的平台一个很好的答案

_kbhit()是在MS Windows最简单的答案。他们的投票()当然是不同

的1是指只有一个描述符块在我的名单中,100是timout

我的文件句柄{0毫秒,对于标准输入

阅读手册页很多你可以询问的事件,我只想要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; 
} 
相关问题