How can I flush unread data from a tty input queue on a UNIX system?

后端 未结 2 1299
礼貌的吻别
礼貌的吻别 2020-12-06 02:11

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

2条回答
  •  有刺的猬
    2020-12-06 02:59

    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.

提交回复
热议问题