标准输入流(pubsetbuf)使用的内部缓冲区 [英] Internal buffer used by standard input stream (pubsetbuf)

查看:149
本文介绍了标准输入流(pubsetbuf)使用的内部缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置输入流的内部缓冲区,但是我在C ++ 17中的实现未为istringstream实现pubsetbuf().

I'm trying to set the internal buffer of an input stream, but my implementation in C++17 doesn't implement pubsetbuf() for istringstream.

我尝试了其他一些技术,但是它们很慢,或者复制了原始缓冲区.我正在寻找一种不做任何复制的快速方法.

I've tried some other techniques, but they are slow or copy the original buffer. I'm looking for a fast method that doesn't do any copying.

这与有关输出流的问题密切相关: 设置标准流使用的内部缓冲区(pubsetbuf)

It's closely related to this question about an output stream: Setting the internal buffer used by a standard stream (pubsetbuf)

我一直在密切关注它,但是输入流的缓冲区仍未初始化/为空.

I've followed it closely, but the buffer for the input stream remains uninitialised/empty.

// Modified template from the other question about an output stream.
// This is for an input stream, but I can't get it to work.
template <typename char_type>
struct istreambuf : public std::basic_streambuf<char_type, std::char_traits<char_type> >
{
    istreambuf(char_type* buffer, std::streamsize buffer_length)
    {
        // Set the "put" pointer to the start of the buffer and record its length.
        this->setp(buffer, buffer + buffer_length);
    }
};

int main()
{
    ifstream infile(FILENAME, std::ifstream::binary);
    if (!infile.is_open())
    {
        cerr << endl << "Failed to open file " << FILENAME << endl;
        return 0;
    }

    // Works, but slow.
    //istringstream local_stream;
    //local_stream << infile.rdbuf();

    // Works, but creates a copy.
    //istringstream local_stream(&buffer[0]);  

    // Works, but creates a copy.
    //local_stream.str(&buffer[0]);

    // Read entire file into buffer.
    infile.seekg(0, std::ios::end);
    streampos length = infile.tellg();
    infile.seekg(0, std::ios::beg);
    vector<char> buffer(length);
    //char* buffer = new char[length];
    infile.read(&buffer[0], length);

    // Doesn't work, but should point to original.
    // It returns "this" (does nothing).
    //local_stream.rdbuf()->pubsetbuf(&buffer[0], length);  

    // Works, but deprecated in C++98.
    //std::istrstream local_stream(&buffer[0]);  
    //local_stream.rdbuf()->pubsetbuf(&buffer[0], length);

    // I followed the example in the other question about an output stream,
    // but I modified for an input stream. I can't get it to work. Any ideas?
    istreambuf<char> istream_buffer(&buffer[0], length);
    istream local_stream(&istream_buffer);

    string str1, str2;
    while (local_stream >> str1 && local_stream >> str2)
    {
        . . .
    }
}

推荐答案

我已经解决了!找出差异.

I've solved it! Spot the difference.

template <typename char_type>
struct istreambuf : public std::basic_streambuf<char_type, std::char_traits<char_type> >
{
    istreambuf(char_type* buffer, std::streamsize buffer_length)
    {
        // Set the "put" pointer to the start of the buffer and record its length.
        //this->setp(buffer, buffer + buffer_length);

        // Set the "get" pointer to the start of the buffer, the next item, and record its length.
        this->setg(buffer, buffer, buffer + buffer_length);
    }
};

我需要设置"get"指针,而不是"put"指针.现在效果很好.

I needed to set the "get" pointer, not the "put" pointer. It works well now.

这篇关于标准输入流(pubsetbuf)使用的内部缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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