我有这个:
//...
if ((tty = open("/dev/tty",O_RDONLY | O_NDELAY) ) == -1 )
{
perror("/dev/tty");
return 1;
}
//...
if (-1 == (fi = open(argv[1], O_RDONLY)) )
{
perror(argv[1]);
return 1;
}
//...
while (1)
{
printf("you have five seconds to enter a line number %d", line);
sleep(5);
i = read(tty, buf, 257);
printf("read %d\n", i);
if ( i == -1) {
perror("dev/tty/");
// if nothing was entered read always returns -1
//and perror prints: Resource temporarily unavailable. it's STRANGE!!!
}
if ( i == 0 )
{
lseek(fi, 0, SEEK_SET);
while((i = read(fi, buf, BUFSIZ)) > 0)
write(1, buf, i);
return 0;
}
//...
if ( 0 == read(fi, buf, lines_length[line]))
{
fprintf(stderr,"can't read\n");
return 1;
}
write(1, buf, lines_length[line]);
}
//...
所以我用 O_NDELAY 标志打开 /dev/tty ,但它似乎有问题,因为稍后读取返回 -1(它必须返回 0)。我不知道出了什么问题。还有一个问题:为什么printf("you have 5 sec")在write(1, buf, lines_length[line]);之后打印