如何使用管道后恢复std :: cin到键盘? [英] how to restore std::cin to keyboard after using pipe?

查看:386
本文介绍了如何使用管道后恢复std :: cin到键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题代码:

#include <array>
#include <iostream>
#include <cstring>

int main(int argc, char *argv[])
{
    using namespace std;

    cout << "Read from file:" << endl;

    while (!cin.eof())
    {
        array<char, 16> l_array;
        cin.read(l_array.data(), l_array.size());
        cout.write(l_array.data(), cin.gcount());
    }

    cout << endl;
    cout << "Read from keyboard:" << endl;

    cin.rdbuf(cout.rdbuf());

    while (!cin.eof())
    {
        array<char, 64> l_array;
        memset(l_array.data(), 0, l_array.size());
        cin.read(l_array.data(), l_array.size());

        cout << "===== DATA =====" << endl;
        cout << l_array.data() << endl;
        cout << "================" << endl;
    }
}

这是我运行程序的方法:

This is how i run my program:

./application < file.txt

我可以从管道读取数据,但是当我想读它时仍与管相关。我不知道如何切换回来。我发现'rdbuf'函数可以改变它,但我不知道如何使用它。

I can read data from pipe without problems but when i want to read it again it is still asociated with pipe. I have no idea how to switch it back. I have found 'rdbuf' function which can change it, but I have no idea how to use it.

我只找到了例子,当你使用键盘切换到文件,

I only found examples when you stard with keyboard switch to file and back to keyboard.

像这样: http://www.cplusplus.com/reference/iostream/ios/rdbuf/

但我没有streambuf记住,所以我可以他们喜欢他们。我想写程序,它可以从文件中读取大多数数据,并且只有当有些东西丢失或只是询问用户在运行时关于permision或某事。所有内部控制台在linux下。

But i don't have streambuf remembered so I can't do it like they did. I want to write program which can read most of data from file, and ask only when something is missing or just to ask user in runtime about permision or something. All inside console under linux.

@EDIT
感谢您的帮助,我后期解决方案

@EDIT Thank you for help, I post solution

class RedirectCinToConsole
{
    protected:
        std::ifstream m_console;
        std::streambuf *m_oldCin;
        bool m_success;

    public:
        RedirectCinToConsole() :
            m_oldCin(0),
            m_success(false)
        {
            m_console.open("/dev/tty");

            if (m_console.is_open())
            {
                m_success = true;
                m_oldCin = std::cin.rdbuf(m_console.rdbuf());
            }
        }
        virtual ~RedirectCinToConsole()
        {
            if (m_oldCin)
            {
                std::cin.rdbuf(m_oldCin);
            }
            m_console.close();
        }

        operator bool () const { return m_success; }
};

int main()
{
    RedirectCinToConsole l_redirect;
    if (l_redirect)
    {
        std::string l_helloWorld;
        std::cin >> l_helloWorld;
        std::cin.ignore();

        std::cout << l_helloWorld;
    }
    return 0;
}


推荐答案

c> / dev / tty 。这将是你的进程的关联控制台,如果有任何。如果您的进程是从守护进程启动,则可能会失败。

Try opening /dev/tty. This will be your process's associated console, if there is any. If your process was started from a daemon, it could fail.

这篇关于如何使用管道后恢复std :: cin到键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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