我怎样才能prevent scanf()的永远等待输入字符? [英] How can I prevent scanf() to wait forever for an input character?

查看:144
本文介绍了我怎样才能prevent scanf()的永远等待输入字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要履行一个控制台应用程序下面的事情:

I want to fulfill the following things in a console application:


  1. 如果用户输入一个字符,应用程序将做
    相应的任务。例如,如果用户输入1时,程序
    会做的任务1,如果用户输入Q,程​​序将退出;

  2. 如果用户输入什么,该方案将执行默认任务每10秒(时间不需要很严格)。

下面是我的code:

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

char buff[64];
char command;

while(command != 'q')
{
   begin:
   printf(">> ");
   scanf("%s", buff);
   command = buff[0];

   switch (command)
   {
       case '1':
       // task 1 code will be added here;
          break;
       case '2':
       // task 2 code will be added here;
          break;
       case 'q':
          printf("quit the loop.\n");
          break;
   }

   // wait for 10 seconds;
   Sleep(10000);
   // default task code will be added here;

  if(command != 'q')
  {
     goto begin;
  }

}

但问题是永远scanf()的函数行程序将陷阱等待输入字符,如果没有输入字符。所以,我不知道如何到一定的时候,我的意思是,例如,如果1秒后没有输入,程序可以继续,后跳过scanf()的线,从而满足上面列出的第二件事。

But the problem is the program will trap at the line of scanf() function forever to wait for an input character, if no character is entered. So I'm wondering how to skip the line of scanf() after a certain time, I mean for example, if no input after 1 second, the program can continue, so as to fulfill the second thing listed above.

我是C语言编程的新手,在此先感谢您的回答。

I'm a newbie of c programming, thanks in advance for your answer.

非常感谢你的亲切答案。
该平台是Windows;
我的同时,(),这是一个明显的错误,后取出分号

推荐答案

尝试使用select()函数。然后你就可以等待10秒,直到你可以从标准输入而不会阻塞读取。如果选择()零返回,执行默认操作。
我不知道这是否适用于Windows,这是POSIX标准。如果你碰巧在Unix / Linux开发,尝试人选择

Try using the select() function. Then you can wait for 10 seconds until you can read from stdin without blocking. If select() returns with zero, perform the default action. I don't know if this works on windows, it's POSIX standard. If you happen to develop on unix/linux, try man select

我刚写使用select工作的例子:

I just wrote a working example using select:

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

#define MAXBYTES 80

int main(int argc, char *argv[])
{
        fd_set readfds;
        int    num_readable;
        struct timeval tv;
        int    num_bytes;
        char   buf[MAXBYTES];
        int    fd_stdin;

        fd_stdin = fileno(stdin);

        while(1) {
                FD_ZERO(&readfds);
                FD_SET(fileno(stdin), &readfds);

                tv.tv_sec = 10;
                tv.tv_usec = 0;

                printf("Enter command: ");
                fflush(stdout);
                num_readable = select(fd_stdin + 1, &readfds, NULL, NULL, &tv);
                if (num_readable == -1) {
                        fprintf(stderr, "\nError in select : %s\n", strerror(errno));
                        exit(1);
                }
                if (num_readable == 0) {
                        printf("\nPerforming default action after 10 seconds\n");
                        break;  /* since I don't want to test forever */
                } else {
                        num_bytes = read(fd_stdin, buf, MAXBYTES);
                        if (num_bytes < 0) {
                                fprintf(stderr, "\nError on read : %s\n", strerror(errno));
                                exit(1);
                        }
                        /* process command, maybe by sscanf */
                        printf("\nRead %d bytes\n", num_bytes);
                        break; /* to terminate loop, since I don't process anything */
                }
        }

        return 0;
}

请注意:下面的民意调查()的例子是也没关系,没问题。对于剩下的我选择了读中可用的字节到缓冲区(最多MAXBYTES)。它可以是扫描(多个)之后。 (scanf()函数只是没有太多的我的朋友,但这是一个个人喜好物质)。

Note: the poll() example below is OK too, no problem. For the rest I chose to read the available bytes into a buffer (up to MAXBYTES). It can be (s)scanned afterwards. (scanf() just isn't too much my friend, but that's a personal taste matter).

这篇关于我怎样才能prevent scanf()的永远等待输入字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆