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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:53:00+00:00 2026-05-24T03:53:00+00:00

I have a Visual Studio 2008 C++ project where I’m trying to serialize data

  • 0

I have a Visual Studio 2008 C++ project where I’m trying to serialize data from several classes to a custom std::streambuf implementation.

The data classes with their serialization:

struct Header { /*...*/ };

inline std::ostream& operator<<( std::ostream& os, const Header& h ) 
{ 
    /* serialize the members of Header here */
    os.write( h.some_member, sizeof( h.some_member ) );
    return os;
}

struct Info { /*...*/ };

inline std::ostream& operator<<( std::ostream& os, const Info& i ) 
{ 
    /* serialize the members of Info here */
    return os;
}

The streambuffer implementation provides buffered i/o and overrides underflow and overflow.

class MyStreamBuf : public std::streambuf
{
public:
    MyStreamBuf() { InitBuffers(); };

private:

    void InitBuffers()
    {
        recv_buffer_.resize( buff_size );
        send_buffer_.resize( buff_size );
        setg( &recv_buffer_[ 0 ], &recv_buffer_[ 0 ], &recv_buffer_[ 0 ] );
        setp( &send_buffer_[ 0 ], &send_buffer_[ 0 ] );
    };

    enum { buff_size = 512 };

    std::vector< char_type > recv_buffer_;
    std::vector< char_type > send_buffer_;

    int_type underflow() { /* ... */ };

    int_type overflow( char_type c )
    {
        *pptr() = c;
        pbump( 1 );
        // if the buffer is full, flush the data
        if( pptr() == epptr() )
            flush();
        return c;
    };

    void flush()
    {
        std::ptrdiff_t n = pptr() - pbase();
        int written = /* send n bytes to the data destination */
        pbump( -written );
    };
}; // class MyStreamBuf

My intended usage is something like this:

Header h( /* some useful info */ );
Info i( /* some useful info */ );

MyStreamBuf dest;
std::iostream my_stream( &dest );
dest << h << i; // MyStreambuf::overflow() is never called!

But, MyStreambuf::overflow is never called. What do I need to do to get the data to my buffer?

Thanks,
PaulH

  • 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-24T03:53:01+00:00Added an answer on May 24, 2026 at 3:53 am

    You need to call flush() on the iostream (or call pubsync() on your streambuf).

    Traditionally you’d do this in the destructor of your streambuf (in the same way that filebuf’s destructor calls close()), in which case you just need to destroy it.

    Also it’s conventional to provide a class inheriting from iostream that owns an instance of your streambuf and ensures it is flushed/destroyed when appropriate.

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

Sidebar

Related Questions

I have a custom build target in a visual studio 2008 c# project. Is
I have a new Visual Studio 2008 Database Project (Data Dude). It was generated
I have a Visual Studio 2008 C++ project that has support for using multiple
I have a Visual Studio 2008 C++ project that outputs a static library and
I have a bunch of Velocity template files in a Visual Studio 2008 project,
I have a .DLL that i include in my Visual Studio 2008 project. The
I have a visual studio 2008 solution that includes an asp.net-hosted remoting project in
I have a Visual Studio 2008 solution with two projects (a Word-Template project and
I have a database project (.dbp) open in Visual Studio 2008 as part of
I have created a setup project using Visual Studio 2008. After the application is

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.