Error[8]: Undefined offset: 2, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我正在完成我的任务,并且必须使用C-Free 5.0.只需要你的帮助来解决这个难题.我想实现一个时间限制,让用户在到期前输入答案.我已经尝试过这段代码,但它在scanf()函数中遇到了阻塞.有没有其他方法,如解锁输入或其他东西.我试图实现’#include< sys / select.h>‘但是这个程序没有那个库. #include <stdio.h> #include <string.h># 我正在完成我的任务,并且必须使用C-Free 5.0.只需要你的帮助来解决这个难题.我想实现一个时间限制,让用户在到期前输入答案.我已经尝试过这段代码,但它在scanf()函数中遇到了阻塞.有没有其他方法,如解锁输入或其他东西.我试图实现’#include< sys / select.h>‘但是这个程序没有那个库.

#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();}
解决方法 这是一个示例程序,显示如何在stdin文件描述符上使用O_NONBLOCK标志.

#include <stdio.h>#include <time.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#define input_LEN 10int 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] = '[+++]';    if(pos > 0)        printf("%s\n",answer);    else        puts("\nSorry,I got tired waiting for your input. Good bye!");}
总结

以上是内存溢出为你收集整理的在标准C的时限内输入全部内容,希望文章能够帮你解决在标准C的时限内输入所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
在标准C的时限内输入_C_内存溢出

在标准C的时限内输入

在标准C的时限内输入,第1张

概述我正在完成我的任务,并且必须使用C-Free 5.0.只需要你的帮助来解决这个难题.我想实现一个时间限制,让用户在到期前输入答案.我已经尝试过这段代码,但它在scanf()函数中遇到了阻塞.有没有其他方法,如解锁输入或其他东西.我试图实现’#include< sys / select.h>‘但是这个程序没有那个库. #include <stdio.h> #include <string.h># 我正在完成我的任务,并且必须使用C-Free 5.0.只需要你的帮助来解决这个难题.我想实现一个时间限制,让用户在到期前输入答案.我已经尝试过这段代码,但它在scanf()函数中遇到了阻塞.有没有其他方法,如解锁输入或其他东西.我试图实现’#include< sys / select.h>‘但是这个程序没有那个库.

#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();}
解决方法 这是一个示例程序,显示如何在stdin文件描述符上使用O_NONBLOCK标志.

#include <stdio.h>#include <time.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#define input_LEN 10int 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] = '';    if(pos > 0)        printf("%s\n",answer);    else        puts("\nSorry,I got tired waiting for your input. Good bye!");}
总结

以上是内存溢出为你收集整理的在标准C的时限内输入全部内容,希望文章能够帮你解决在标准C的时限内输入所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1226394.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存