简体   繁体   English

这个流缓冲区出了什么问题?

[英]What's wrong with this stream buffer?

What's wrong with my overflow() here. 我的overflow()在这里出了什么问题。 When I print oss.str() it prints "Hello, Wor" instead of "Hello, World" . 当我打印oss.str()它打印"Hello, Wor"而不是"Hello, World" What did I do wrong? 我做错了什么?

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

class io_buffer : public std::streambuf
{
public:
    io_buffer(std::ostream& os, int buf_size = 4)
        : os_(os), buffer(buf_size)
    {
        os_.clear();
        char* beg = buffer.data();
        setp(beg, beg + buffer.size());
    }

    int_type overflow(int_type c)
    {
        if (os_ && !traits_type::eq_int_type(c, traits_type::eof()))
        {
            *pptr() = traits_type::to_char_type(c);
            pbump(1);

            if (flush())
            {
                setp(buffer.data(), buffer.data() + buffer.size());
                return c;
            } else
                return traits_type::eof();
        }

        return traits_type::not_eof(c);
    }

    bool flush()
    {
        return os_.write(pbase(), pptr() - pbase());
    }

    int sync()
    {
        return flush() ? 0 : -1;
    }
private:
    std::ostream& os_;
    std::vector<char> buffer;
};

int main()
{
    std::ostringstream oss;
    io_buffer buf(oss);

    std::ostream os(&buf);
    std::string str("Hello, World");

    os << str;
    std::cout << oss.str() << std::endl;
}

You need to flush also a std::vector (buffer), ie: 你还需要刷新std::vector (缓冲区),即:

int_type overflow(int_type c)
    {
        if (os_ && !traits_type::eq_int_type(c, traits_type::eof()))
        {
            *pptr() = traits_type::to_char_type(c);
            pbump(1);

            if (flush())
            {
                buffer.clear();  // <-
                setp(buffer.data(), buffer.data() + buffer.size());
                return c;
            } else
                return traits_type::eof();
        }

        return traits_type::not_eof(c);
    }

Even better, as 0x499602D2 address suggested use pbump(-buffer.size()) to avoid multiple calls to overflow() . 更好的是,因为0x499602D2地址建议使用pbump(-buffer.size())来避免多次调用overflow()

The problem is that you use: 问题是你使用:

setp(beg, beg + buffer.size());

and in overflow() you add new element with no reallocation, the end pointer should be accessible (if you dont want to reallocate), otherwise you need to reallocate your buffer in overflow(). 并且在overflow()中添加没有重新分配的新元素,结束指针应该是可访问的(如果你不想重新分配),否则你需要在overflow()中重新分配你的缓冲区。 So change it to: 所以改成它:

setp(beg, beg + buffer.size() - 1);

in io_buffer constructor 在io_buffer构造函数中

and later in overflow change setp to: 然后在溢出更改setp到:

pbump(-(pptr() - pbase()));

also to flush your buffer add endl: 还要刷新缓冲区添加endl:

os << str << endl;

working example: http://coliru.stacked-crooked.com/a/7c72ecfe78bb2aee 工作示例: http//coliru.stacked-crooked.com/a/7c72ecfe78bb2aee

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM