Composite std::istream in C++

橙三吉。 提交于 2019-12-11 02:47:55

问题


I have a list of std::istream objects that I need to appear as a single std::istream object. So if I have three istreams, A, B and C, I want to be able to create an istream, D which will first return the bytes from A then the bytes from B, then C before EOF is reached. The composite stream will always be read sequentially and closed after all bytes are read.

Is there a simple way to do this using stl/boost or do I just need to write my own composite istream?


回答1:


The off-topic answer to another question (https://stackoverflow.com/a/17103292/1424877) might help you.

#include <iostream>
#include <string>
#include <sstream>

class ConcatStreams : public std::streambuf
{
    int useBuf;
    std::streambuf *sbuf_[2];
    char buffer_[1024];

  public:
    ConcatStreams(std::istream& sbuf1, std::istream& sbuf2) :
        useBuf(0), sbuf_{sbuf1.rdbuf(), sbuf2.rdbuf()}
    { }

    int underflow()
    {
        if (this->gptr() == this->egptr()) {
            std::streamsize size = 0;
            while (useBuf < 2) {
                size = this->sbuf_[useBuf]->sgetn(this->buffer_, sizeof this->buffer_);
                if (!size) {
                    useBuf++;
                } else {
                    break;
                }
            } 
            this->setg(this->buffer_, this->buffer_, this->buffer_ + size);
        }
        return this->gptr() == this->egptr()
             ? std::char_traits<char>::eof()
             : std::char_traits<char>::to_int_type(*this->gptr());
    }
};

int main()
{
    std::istringstream is("hello world!\n");

    ConcatStreams cs_(is, std::cin); // prepend "hello world" to the input
    std::istream cs(&cs_);

    std::string s;
    while (cs >> s)
      std::cout << "'" << s << "'" << std::endl;
}

Notice that you can't use this particular trick to concatenate std::cin with itself, or even is with itself; but it should work fine to concatenate any two different input streams. You can even concatenate multiple ConcatStreams instances!



来源:https://stackoverflow.com/questions/19506444/composite-stdistream-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!