std :: cin:清空输入缓冲区而不会阻塞

花园巨人

这个问题的后续行动

如何清除输入缓冲区?

sleep(2);
// clear cin
getchar();

我只想要最后输入的字符,我想放弃程序睡眠时输入的所有输入。有没有办法做到这一点?

另外,我不想主动等待清除cin,因此无法使用ignore()。

我的方法是获取缓冲区的当前大小,如果不等于0,则静默清空缓冲区。但是,我还没有找到获取缓冲区大小的方法。std :: cin.rdbuf()-> in_avail()总是为我返回0。peek()也积极等待。

我不想使用ncurses。

用户名

具有支持tcgetattr / tcsetattr的系统:

#include <iostream>
#include <stdexcept>

#include <termios.h>
#include <unistd.h>

class StdInput
{
    // Constants
    // =========

    public:
    enum {
        Blocking = 0x01,
        Echo = 0x02
    };

    // Static
    // ======

    public:
    static void clear() {
        termios attributes = disable_attributes(Blocking);
        while(std::cin)
            std::cin.get();
        std::cin.clear();
        set(attributes);
    }

    // StdInput
    // ========

    public:
    StdInput()
    :   m_restore(get())
    {}

    ~StdInput()
    {
        set(m_restore, false);
    }

    void disable(unsigned flags) { disable_attributes(flags); }
    void disable_blocking() { disable_attributes(Blocking); }
    void restore() { set(m_restore); }

    private:
    static termios get() {
        const int fd = fileno(stdin);
        termios attributes;
        if(tcgetattr(fd, &attributes) < 0) {
            throw std::runtime_error("StdInput");
        }
        return attributes;
    }

    static void set(const termios& attributes, bool exception = true) {
        const int fd = fileno(stdin);
        if(tcsetattr(fd, TCSANOW, &attributes) < 0 && exception) {
            throw std::runtime_error("StdInput");
        }
    }

    static termios disable_attributes(unsigned flags) {
        termios attributes = get();
        termios a = attributes;
        if(flags & Blocking) {
            a.c_lflag &= ~ICANON;
            a.c_cc[VMIN] = 0;
            a.c_cc[VTIME] = 0;
        }
        if(flags & Echo) {
            a.c_lflag &= ~ECHO;
        }
        set(a);
        return attributes;
    }

    termios m_restore;
};


int main()
{
    // Get something to ignore
    std::cout << "Ignore: ";
    std::cin.get();

    // Do something

    StdInput::clear();

    std::cout << " Input: ";
    std::string line;
    std::getline(std::cin, line);
    std::cout << "Output: " << line << std::endl;

    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

std :: cin:清空输入缓冲区而不会阻塞

来自分类Dev

我可以循环直到 `std::cin` 在输入缓冲区中看到 `\n` 字符吗?

来自分类Dev

使用 std::cin.getline() 后如何保留 std::cin 的缓冲区

来自分类Dev

istream std :: cin如何修改自定义istream缓冲区?

来自分类Dev

从std :: vector的缓冲区CV :: imwrite

来自分类Dev

std :: stringstream缓冲区操作

来自分类Dev

用std :: copy复制缓冲区

来自分类Dev

C ++使用std :: string,std :: wstring作为缓冲区

来自分类Dev

STD文件流已损坏?(内部文件缓冲区为空,无法读取输入)

来自分类Dev

如何使用std :: hash哈希缓冲区?

来自分类Dev

如何保存和恢复std :: istringstream的缓冲区?

来自分类Dev

直接写入std :: string的char *缓冲区

来自分类Dev

直接写入std :: strings缓冲区是否安全?

来自分类Dev

如何保存和恢复std :: istringstream的缓冲区?

来自分类Dev

检查cin输入,清除输入缓冲区

来自分类Dev

std :: vector <std :: vector <int >> push_back导致堆缓冲区溢出

来自分类Dev

在std :: string :: end()和std :: string :: capacity()之间使用缓冲区

来自分类Dev

std :: cin不会在输入错误时抛出异常

来自分类Dev

非阻塞或阻塞分配的缓冲区?

来自分类Dev

分叉进程时std :: cin不阻塞

来自分类Dev

成功接收后清空缓冲区

来自分类Dev

从空缓冲区构造`std :: ostream`是否有效?

来自分类Dev

是否允许在C ++ 11中修改operator []返回的内部std :: string缓冲区

来自分类Dev

C ++ 11 std :: string :: operator []是否将返回以空值结尾的缓冲区

来自分类Dev

std :: stringstream直接输出缓冲区/字符串结果访问,避免复制?

来自分类Dev

如何清除(用随机字节覆盖)std :: string内部缓冲区?

来自分类Dev

将缓冲区或C字符串复制到std :: string的最快方法

来自分类Dev

将std :: vector用作简单缓冲区是一种好习惯吗?

来自分类Dev

将字符缓冲区传递到std :: string()后是否需要释放它?

Related 相关文章

  1. 1

    std :: cin:清空输入缓冲区而不会阻塞

  2. 2

    我可以循环直到 `std::cin` 在输入缓冲区中看到 `\n` 字符吗?

  3. 3

    使用 std::cin.getline() 后如何保留 std::cin 的缓冲区

  4. 4

    istream std :: cin如何修改自定义istream缓冲区?

  5. 5

    从std :: vector的缓冲区CV :: imwrite

  6. 6

    std :: stringstream缓冲区操作

  7. 7

    用std :: copy复制缓冲区

  8. 8

    C ++使用std :: string,std :: wstring作为缓冲区

  9. 9

    STD文件流已损坏?(内部文件缓冲区为空,无法读取输入)

  10. 10

    如何使用std :: hash哈希缓冲区?

  11. 11

    如何保存和恢复std :: istringstream的缓冲区?

  12. 12

    直接写入std :: string的char *缓冲区

  13. 13

    直接写入std :: strings缓冲区是否安全?

  14. 14

    如何保存和恢复std :: istringstream的缓冲区?

  15. 15

    检查cin输入,清除输入缓冲区

  16. 16

    std :: vector <std :: vector <int >> push_back导致堆缓冲区溢出

  17. 17

    在std :: string :: end()和std :: string :: capacity()之间使用缓冲区

  18. 18

    std :: cin不会在输入错误时抛出异常

  19. 19

    非阻塞或阻塞分配的缓冲区?

  20. 20

    分叉进程时std :: cin不阻塞

  21. 21

    成功接收后清空缓冲区

  22. 22

    从空缓冲区构造`std :: ostream`是否有效?

  23. 23

    是否允许在C ++ 11中修改operator []返回的内部std :: string缓冲区

  24. 24

    C ++ 11 std :: string :: operator []是否将返回以空值结尾的缓冲区

  25. 25

    std :: stringstream直接输出缓冲区/字符串结果访问,避免复制?

  26. 26

    如何清除(用随机字节覆盖)std :: string内部缓冲区?

  27. 27

    将缓冲区或C字符串复制到std :: string的最快方法

  28. 28

    将std :: vector用作简单缓冲区是一种好习惯吗?

  29. 29

    将字符缓冲区传递到std :: string()后是否需要释放它?

热门标签

归档