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

  • SEARCH
  • Home
  • 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 7502597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:55:26+00:00 2026-05-29T20:55:26+00:00

I am using write( fd, buffer, count ) in io.h to write buffer into

  • 0

I am using write( fd, buffer, count ) in io.h to write buffer into a file descriptor.

Now I found huge performance issue, because every time I call “write” it will perform a IO operation.

I want to add a buffer before actually writing content into file.

What is the best way to do this?

I searched and found this:

http://www.java2s.com/Tutorial/Cpp/0240__File-Stream/filedescriptoroutbuffer.htm

Should I use this method and integrate into my code? Is that a good approach I should take?

But in it, I don’t see how do define the buffer size.

Following is the final code:

class fdoutbuf : public std::streambuf {
    protected:
        enum { size = 4096 };
        int fd;    // file descriptor
        char buf_[size];
    public:
    // constructor
    fdoutbuf (int _fd) : fd(_fd) {
        setp(this->buf_, this->buf_ + size - 1);
    }
    protected:
    // write one character
    virtual int overflow (int c){
        if (c != EOF) {
            char z = c;
            *this->pptr() = z;
            this->pbump(1);
        }
        return this->sync() == -1? EOF: c;
    }
    virtual int sync(){
         if (this->pbase() == this->pptr()) {
            return 0;
        }
        int count(this->pptr() - this->pbase());
        int rc = write(fd, this->buf_, count);
        this->setp(this->buf_, this->buf_ + size - 1);
        return count == rc? 0: -1;
    }
};


class fdostream : public std::ostream {
  protected:
    fdoutbuf buf;
  public:
    fdostream (int fd) : std::ostream(0), buf(fd) {
        rdbuf(&buf);
    }
};
  • 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-29T20:55:33+00:00Added an answer on May 29, 2026 at 8:55 pm

    Creating the buffering infrastructure isn’t really rocket science but also isn’t entirely trivial. Personally, I would just create a class derived from std::streambuf and either use the stream buffer abstraction or a stream (probably I would use the latter and accept the small performance impact introduced by most implementations). The work need to do this is fairly simple (this is for writing only):

    struct fdbuf: std::streambuf {
        enum { size = 4096 };
        fdbuf(int fd): fd_(fd) { this->setp(this->buf_, this->buf_ + size - 1); }
        int overflow(int c) {
            if (!traits_type::eq_int_type(c, traits_type::eof())) {
                *this->pptr() = traits_type::to_char_type(c);
                this->pbump(1);
            }
            return this->sync() == -1? traits_type::eof(): traits_type::not_eof(c);
        }
        int sync() {
            if (this->pbase() == this->pptr()) {
                return 0;
            }
            int count(this->pptr() - this->pbase());
            int rc = write(this->fd_, this->buf_, count);
            this->setp(this->buf_, this->buf_ + size - 1);
            return count == rc? 0: -1;
        }
        int  fd_;
        char buf_[size];
    };
    

    It seems it may be worth explaining what the stream buffer above actually does (given the lengthy discussion below). So, here is a breakdown:

    • The code uses a fixed size buffer. This is just to reduce the size of the example: it can be easily extended to use a variable sized buffer which is allocated e.g. using a std::vector<char>: the size would become a default parameter.
    • the function setp() sets up a buffer used by the stream buffer, consisting of three pointers:
      • pbase() is the start of the buffer, i.e. the first parameter to setp()
      • epptr() is the end of the buffer, i.e. the second parameter to setp()
      • pptr() is set to the same value as pbase() but is the pointer which is actually moved to indicate the position of the next character. If this is epptr() when a character is written overflow() is called.
    • The virtual function overflow() is possibly given the character which caused the buffer to overflow. Unless it is called by user code with an argument of eof() this is the case when the standard library calls the function. To deal with this extra character, the buffer is given one element less there is available space: the character can be appended to the buffer before the buffer is actually written.
    • The traits_type inherited from std::streambuf defines a number of functions to determine properties of characters. When checking the parameter passed to overflow() the function eq_int_type() is used which compares to objects of the stream’s integer type for equality. It compares the argument to overflow() with the value indicating an invalid character which is obtained using eof(). All members of the traits type are static or typedefs, i.e. there is no need to use an object of this type. The use of the traits_type is important when using different template instantiations of std::basic_streambuf.
    • Although the buffer is normally pbase() to epptr() with pptr() between these two ends, pptr() can be moved beyond epptr() using pbump(n): this function just adds n to pptr(). This is used to put the currecnt character into the buffer right before calling sync() which just writes the content of the buffer and resets it.
    • The return of overflow() is eof() if the function failed. On successful execution the function returns something different than eof() and conventionally the character passed as argument is used. As the argument may be eof() it isn’t good enough to just return the argument, though: if it is eof() this value needs to be transformed into some other useful value which is what not_eof() does: it returns it arguments unless the argument is eof() in which case it returns some suitable other value.
    • The virtual function sync() is responsible for flushing the buffer. In this case it is first checking that there is something to be done and, if so, writes the content of the buffer. There isn’t really any trick in this function although I guess a proper one would be more careful if the write() fails, i.e. returns fewer characters than are characters in the buffer. This version just pretends that it is OK to loose characters which couldn’t be written although it will indicate an error.
    • The function still resets the buffer using the same approach of leaving one character space at the end of the buffer.
    • Upon failure the function returns -1, on success it return 0. No trick.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using read/write locks on Linux and I've found that trying to upgrade a
I'm using FileStream to write to a file, and watching the underlying system calls
I am writing the data to the output browser using Response.write(some byte arrary) Response.ContentType
I'm downloading a vCard to the browser using Response.Write to output .NET strings with
In C#, how many classes can you inherit from? When you write: using System.Web.UI;
When using copy-on-write semantics to share memory among processes, how can you test if
I'm using Jython to write tests for a Java project. It works well, but
I am using XmlSerializer to write and read an object to xml in C#.
I'm using Django to write a blog app, and I'm trying to implement a
I have a GridView and I intend to export it into .xls file. Paging

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.