我想在控制台应用程序中完成以下事项:
1,程序将执行任务1,如果用户输入q,程序将退出;这是我的代码:
#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;
}
}
Run Code Online (Sandbox Code Playgroud)
但问题是scanf()如果没有输入字符,程序将永远陷入功能行等待输入字符.所以我想知道如何跳过scanf()一段时间之后的行,我的意思是,例如,如果1秒后没有输入,程序可以继续,以便完成上面列出的第二件事.
该平台是Windows,如果重要的话.
while()这是一个明显的错误,我删除了分号.
Ron*_*ald 11
尝试使用select()函数.然后你可以等待10秒,直到你可以不受阻塞地读取标准输入.如果select()返回零,则执行默认操作.我不知道这是否适用于Windows,它是POSIX标准.如果您碰巧在unix/linux上开发,请尝试man select
我刚刚用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;
}
Run Code Online (Sandbox Code Playgroud)
注意:下面的poll()示例也没问题,没问题.其余的我选择将可用字节读入缓冲区(最多MAXBYTES).之后可以进行扫描.(scanf()只是我的朋友不是太多,但这是个人品味问题).
这是一个如何使用的工作示例poll(可能是Linux上最"正确"的方式):
#include <unistd.h>
#include <poll.h>
#include <stdio.h>
int main()
{
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
char string[10];
if( poll(&mypoll, 1, 2000) )
{
scanf("%9s", string);
printf("Read string - %s\n", string);
}
else
{
puts("Read nothing");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
超时是第三个参数,poll以毫秒为单位 - 此示例将在stdin上等待2秒.Windows有WSAPoll,应该类似的工作.
但问题是程序将永远陷入 scanf() 函数行以等待输入字符,
去掉后面的分号while。