我怎么能刷新在UNIX系统上从一个tty输入队列读出的数据? [英] How can I flush unread data from a tty input queue on a UNIX system?

查看:127
本文介绍了我怎么能刷新在UNIX系统上从一个tty输入队列读出的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序必须读取从标准输入一个字符,所以我用阅读(0,缓冲,1)
但是,如果用户插入多个单个字符,他们仍然在一些缓冲,当我称之为他们又仍然存在。

My program has to read just ONE character from the standard input, and so I use read(0, buffer, 1). But if the user insert more than one single character, they remain in some buffer and when I call a read again they are still there.

所以,我怎么能放弃这些字符?
我想,当我称之为再次,缓冲区充满了新的角色,不与旧的。

So, how can I discard these characters? I want that when I call a read again, the buffer is filled with the new character, not with the old ones.

一个例子:
我有一个的 阅读(0,缓冲,1) 的和用户写的 ABCDE 的。我的缓冲区包含的的(而且是正确的),但后来我打电话的 阅读(0,缓冲,1) 的再我想从现在用户写入下一个字符,而不是的的书面前。

An example: I've a read(0, buffer, 1) and the user writes abcde. My buffer contains a (and it's right), but then I call read(0, buffer, 1) again and I want the next character written by the user from now, and not the b written before.

推荐答案

在你的程序要开始读取字符,就必须排出现有的字符缓冲区,然后等待读取字符。

When your program wants to start reading characters, it must drain the buffer of existing characters and then wait to read the character.

否则,它会读取最后输入的字符,而不是最后一个字符后进入的现在

Otherwise, it will read the last character entered, not the last character entered after right now.

当然,你并不需要读人物做任何事情;但是,你需要阅读。

Naturally, you do not need to do anything with the read characters; but, you do need to read them.

这篇关于我怎么能刷新在UNIX系统上从一个tty输入队列读出的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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