在标准C的时限内输入 [英] Input within a time limit in Standard C

查看:56
本文介绍了在标准C的时限内输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在做我的作业,它是强制使用C-免费5.0。只需要你的帮助来解决这一块拼图。我想,以实现对用户输入到期前回答的时间限制。我已经试过这code,但它在scanf()的功能得到了块。有像解封输入或东西的任何其它方法。我试图实施的#include< SYS / select.h> '。但这个方案没有该库

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&time.h中GT;
#包括LT&;&stdlib.h中GT;诠释的main()
{
    炭ST [10];
    的printf(请输入一行文字:);
    time_t的结束时间=(0)+ 5; //时间5秒的限制。
    而(时间(0)LT端)
    {
        scanf函数(%S,&安培; ST);
        如果(ST!= NULL)
        {
            的printf(谢谢你,你进入>%S< \\ N,ST);
            出口(0);
        }
    }
    主要();
}


解决方案

下面是一个例子程序,说明如何使用 O_NONBLOCK 标志对标准输入文件描述符。

 的#include<&stdio.h中GT;
#包括LT&;&time.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&fcntl.h GT;#定义INPUT_LEN 10诠释的main()
{
    的printf(请输入一行文字:);
    fflush(标准输出);
    time_t的结束时间=(0)+ 5; //时间5秒的限制。    INT标志=的fcntl(STDIN_FILENO,F_GETFL,0);
    的fcntl(STDIN_FILENO,F_SETFL,旗帜| O_NONBLOCK);    焦炭答案[INPUT_LEN]
    INT POS = 0;
    而(时间(0)LT端)
    {
        INT C =的getchar();        / * 10是新行* /
        如果(!C = EOF和放大器;和C = 10安培;&放大器; POS&L​​T; INPUT_LEN - 1)
            回答[POS ++] = C;        / *如果有新的行中输入我们已经准备好* /
        如果(C == 10)
            打破;
    }    回答[POS] ='\\ 0';    如果(POS大于0)
        的printf(%S \\ n,答案);
    其他
        看跌期权(\\ nSorry,我厌倦了等待你输入再见!);
}

I'm currently doing my assignment and it's compulsory to use C-Free 5.0. Just need your help to solve this piece of puzzle. I want to implement a time limit for the user to input an answer before it expires. I've tried this code but it got block at scanf() function. Is there any other method like an unblocking input or something. I've tried to implement '#include <sys/select.h>' but this program doesn't have that library.

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

int main()
{
    char st[10];
    printf ("Please enter a line of text : ");
    time_t end = time(0) + 5; //5 seconds time limit.
    while(time(0) < end)
    {
        scanf("%s", &st);
        if(st != NULL)
        {
            printf ("Thank you, you entered >%s<\n", st);
            exit(0);
        }
    }
    main();
}

解决方案

Here is an example program that shows how you can use O_NONBLOCK flag on a stdin file descriptor.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#define INPUT_LEN 10

int main()
{
    printf ("Please enter a line of text : ");
    fflush(stdout);
    time_t end = time(0) + 5; //5 seconds time limit.

    int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
    fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);

    char answer[INPUT_LEN];
    int pos = 0;
    while(time(0) < end)
    {
        int c = getchar();

        /* 10 is new line */
        if (c != EOF && c != 10 && pos < INPUT_LEN - 1)
            answer[pos++] = c;

        /* if new line entered we are ready */
        if (c == 10)
            break;
    }

    answer[pos] = '\0';

    if(pos > 0)
        printf("%s\n", answer);
    else
        puts("\nSorry, I got tired waiting for your input. Good bye!");
}

这篇关于在标准C的时限内输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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