Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6006895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:36:18+00:00 2026-05-23T01:36:18+00:00

You can specify one buffer for your file stream like that: char buf[BUFFER_SIZE]; std::ofstream

  • 0

You can specify one buffer for your file stream like that:

char buf[BUFFER_SIZE];

std::ofstream file("file", std::ios_base::binary | std::ios_base::out);
if (file.is_open())
{
    file.rdbuf()->pubsetbuf(buf, BUFFER_SIZE);
    file << "abcd";
}

What I want to do now, is using more than just one buffer:

char* buf[] = { new char[BUFFER_SIZE], new char[BUFFER_SIZE], new char[BUFFER_SIZE], };

Is it possible without creating a custom derivation of std::streambuf?

EDIT:
I think I need to explain what I want to do in more detail. Please consider the following situation:
– The file(s) I want to read won’t fit into memory
– The file while be accessed by some kind of a binary jump search

So, if you split the file into logical pages of a specific size, then I would like to provide multiple buffers which are representing specific pages. This would increase performance when a file location is read and the related page is already in a buffer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:36:19+00:00Added an answer on May 23, 2026 at 1:36 am

    I will take a look at boost::iostreams::mapped_file, but I think my requirement is much simpler. I’ve created a custom class derived from basic_filebuf.

    template<typename char_type>
    class basic_filemultibuf : public std::basic_filebuf<char_type/*, std::char_traits<char_type>*/>
    {
    private:
        char_type**     m_buffers;
        std::ptrdiff_t  m_buffer_count,
                        m_curent_buffer;
        std::streamsize m_buffer_size;
    
    protected:
        virtual int_type overflow(int_type meta = traits_type::eof())
        {
            if (this->m_buffer_count > 0)
            {
                if (this->m_curent_buffer == this->m_buffer_count)
                    this->m_curent_buffer = 0;
                this->basic_filebuf::setbuf(this->m_buffers[this->m_curent_buffer++], this->m_buffer_size);
            }
    
            return this->basic_filebuf::overflow(meta);
        }
    
    public:
        basic_filemultibuf(basic_filebuf const& other)
            : basic_filebuf(other),
              m_buffers(NULL),
              m_buffer_count(0),
              m_curent_buffer(-1),
              m_buffer_size(0)
        {
        }
    
        basic_filemultibuf(basic_filemultibuf const& other)
            : basic_filebuf(other),
              m_buffers(other.m_buffers),
              m_buffer_count(other.m_buffer_count),
              m_curent_buffer(other.m_curent_buffer),
              m_buffer_size(other.m_buffer_size)
        {
        }
    
        basic_filemultibuf(FILE* f = NULL)
            : basic_filemultibuf(basic_filebuf(f))
        {
        }
    
        basic_filemultibuf* pubsetbuf(char** buffers, std::ptrdiff_t buffer_count, std::streamsize buffer_size)
        {
            if ((this->m_buffers = buffers) != NULL)
            {
                this->m_buffer_count  = buffer_count;
                this->m_buffer_size   = buffer_size;
                this->m_curent_buffer = 0;
            }
            else
            {
                this->m_buffer_count  = 0;
                this->m_buffer_size   = 0;
                this->m_curent_buffer = -1;
            }
    
            this->basic_filebuf::setbuf(NULL, 0);
            return this;
        }
    };
    

    Example usage:

    typedef basic_filemultibuf<char> filemultibuf;
    
    std::fstream file("file", std::ios_base::binary | std::ios_base::in | std::ios_base::out);
    
    char** buffers = new char*[2];
    for (int i = 0; i < n; ++i)
        buffers[i] = new char[4096];
    
    filemultibuf multibuf(*file.rdbuf());
    multibuf.pubsetbuf(buffers, 2, 4096);
    file.set_rdbuf(&multibuf);
    
    //
    // do awesome stuff with file ...
    //
    
    for (int i = 0; i < n; ++i)
        delete[] buffers[i];
    

    That’s pretty much it. The only thing I would really like to do is offer this functionally for other streambufs, because the usage of multiple buffers should not be restricted to filebuf. But it seems to me it isn’t possible without rewriting the file specific functions.

    What do you think about that?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can one specify the connection string in a config file of a class
Using the WCF web programming model one can specify an operation contract like so:
In Linux, one can specify the system's default receive buffer size for network packets,
I know I can specify one for each form, or for the root form
Can one specify XML attribute values as CDATA ? If yes - what would
Can some one specify the windows API, one need to use in order to
I understand that I can specify system properties to Tomcat by passing arguments with
I have an application that manages documents called Notes. Like a blog, Notes can
In Subversion you can specify a range of versions to get an aggregate view
I know you can specify fieldsets in django for Admin helpers. However, I cannot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.