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 9113433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:57:49+00:00 2026-06-17T03:57:49+00:00

I need some guidance or pointers understanding how to implement a custom ostream. My

  • 0

I need some guidance or pointers understanding how to implement a custom ostream. My requirements are:

  1. A class with a ‘<<‘ operator for several data types.
  2. The intention is to send output to database. Each “line” should go to a separate record.
  3. Each record most important field would be the text (or blob), but some other fields such as time, etc. can be mostly deduced automatically
  4. buffering is important, as I don’t want to go to database for every record.

First, does it worth deriving from ostream? What do I get by deriving from ostream? What if my class simply implements few operator<< methods (including some custom data types). Which functionality do I get from ostream?

Assuming what I want is a class derived from ostream, I need some guidance understanding the relationship between the ostream and the streambuf classes. Which one do I need to implement? Looking at some samples, it appears that I don’t need to derive from ostream at all, and just give the ostream constructor a custom streambuf. Is that true? is that the canonical approach?

Which virtual functions at the custom streambuf do i need to implement? I’ve seen some samples (including this site: here and here, and few more), some override the sync method, and other override the overflow method. Which one should I override? Also, looking at the stringbuf and filebuf sources (Visual Studio or GCC) both those buffer classes implement many methods of the streambuf.

If a custom class derived from streambuf is required, would there be any benefit deriving from stringbuf (or any other class) instead of directly from streambuf?

As for “lines”. I would like at least when my users of the class using the ‘endl’ manipulator to be a new line (i.e. record in database). Maybe – depends on effort – every ‘\n’ character should be considered as a new record as well. Who do my custom ostream and/or streambuf get notified for each?

  • 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-06-17T03:57:50+00:00Added an answer on June 17, 2026 at 3:57 am

    A custom destination for ostream means implementing your own ostreambuf. If you want your streambuf to actually buffer (i.e. don’t connect to the database after each character), the easiest way to do that is by creating a class inheriting from std::stringbuf. The only function that you’ll need to override is the sync() method, which is being called whenever the stream is flushed.

    class MyBuf : public std::stringbuf
    {
    public:
        virtual int sync() {
            // add this->str() to database here
            // (optionally clear buffer afterwards)
        }
    };
    

    You can then create a std::ostream using your buffer:

    MyBuf buff;
    std::ostream stream(&buf)
    

    Most people advised against redirecting the stream to a database, but they ignored my description that the database basically has a single blob field where all text is going to.
    In rare cases, I might send data to a different field. This can be facilitated with custom attributes understood by my stream. For example:

    MyStream << "Some text " << process_id(1234) << "more text" << std::flush
    

    The code above will create a record in the database with:

    blob: 'Some text more text'
    process_id: 1234
    

    process_id() is a method returning a structure ProcessID. Then, in the implementation of my ostream, I have an operator<<(ProcessID const& pid), which stores the process ID until it gets written. Works great!

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

Sidebar

Related Questions

I need some guidance on how to save video data to core data so
I need some guidance/advices from someone who has any ideea about custom jquery event.
Im new to database design and need some guidance. A lot of new data
I am new to iphone development and need some guidance to do custom sorting
I need some guidance on how to create a custom table cell for my
Need some guidance figuring out what went wrong. I've been using mysql, phpmyadmin for
I need some guidance around which approach to use to load binary files from
I am new to mocking and dependency injection and need some guidance. My application
I am new in this field and i desperately need some guidance from u
Really need just some guidance : Topological sort by arcs definition (from my question)

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.