Linux下C选择:管道回声输入的作品,但是从键盘读不? [英] Linux C select: piping echo to input works, but reading from keyboard doesn't?

查看:96
本文介绍了Linux下C选择:管道回声输入的作品,但是从键盘读不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解 http://beej.us/guide/bgnet/examples /select.c (包括如下供参考)。我这样做:

I am trying to understand http://beej.us/guide/bgnet/examples/select.c (included below for reference). I am doing this:


    :〜$猫的/ etc /问题

:~$ cat /etc/issue

Ubuntu 10.04 LTS \n \l
:~$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3

:~$ wget http://beej.us/guide/bgnet/examples/select.c
:~$ gcc select.c -o select

:~$ echo "ff" | ./select 
A key was pressed!

:~$ ./select 
TYPINGTYTimed out.

所以,选择程序显然认识到回声管道成其为输入;但它不会在终端上识别键presses。为什么是这样?可以某种重定向的使用(我猜的,类似于如何能屏重定向键盘输入到串行会话),以便在终端实际密钥presses被识别?

So, the select program apparently recognizes an echo piping into it as input; but it will not recognize keypresses on the terminal. Why is this? Can some sort of redirection be used (I guess, similar to how screen could 'redirect' keyboard input to a serial session) so that actual key presses in terminal are recognized?

谢谢,
干杯!

Thanks, Cheers!

select.c:


    / *
    ** select.c - 一个select()演示
    * /

/* ** select.c -- a select() demo */

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#define STDIN 0  // file descriptor for standard input

int main(void)
{
 struct timeval tv;
 fd_set readfds;

 tv.tv_sec = 2;
 tv.tv_usec = 500000;

 FD_ZERO(&readfds);
 FD_SET(STDIN, &readfds);

 // don't care about writefds and exceptfds:
 select(STDIN+1, &readfds, NULL, NULL, &tv);

 if (FD_ISSET(STDIN, &readfds))
  printf("A key was pressed!\n");
 else
  printf("Timed out.\n");

 return 0;
}






编辑:看到应允。因此,所有我们需要的是preSS输入:

see answer; thus all we need is to press enter:

:~$ ./select 

A key was pressed!

或者我们可以关闭缓冲输入与 stty的原料(并重新打开 stty的熟):

or we can turn off buffered input with stty raw (and turn it back on with stty cooked):

:~ stty raw
:~ ./select 
                                            dA key was pressed!
                                                               :~ stty cooked 

推荐答案

标准输入是一个缓冲流。在select()调用将无法检测到存在可用的输入,直到换行是在输入结束打击。你不能使用select()这样的阅读个别按键。

Standard input is a buffered stream. The select() call will not be able to detect that there is input available until the newline is hit at the end of the input. You can't use select() like this to read individual keystrokes.

这篇关于Linux下C选择:管道回声输入的作品,但是从键盘读不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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