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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:29:29+00:00 2026-06-12T23:29:29+00:00

I have a simple GUI program that uses a custom stringstream to redirect output

  • 0

I have a simple GUI program that uses a custom stringstream to redirect output from the console to a text field in the GUI (under some circumstances). currently. the window redraws any time I hit enter, but it’s possible that output could be generated at other times. Is there a way to register a function with the stringstream that gets executed every time the << operator is used on the stream?

NOTE

I should have pointed out that I cannot use C++11 in my solution. the machines on which this will be compiled and run will not have c++11 available.

  • 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-12T23:29:33+00:00Added an answer on June 12, 2026 at 11:29 pm

    Personally, I wouldn’t use an std::ostringstream (or even an std::stringstream) for this at all! Instead, I would create my own stream buffer taking care of sending the data to the GUI. That is, I’d overwrite std::streambuf::overflow() and std::streambuf::sync() to send the current data to the GUI. To also make sure that any output is sent immediately, I’d set up an std::ostream to have std::ios_base::unitbuf set. Actually, sending the changes to a function is quite simple, i.e., I’ll implement this:

    #include <streambuf>
    #include <ostream>
    #include <functional>
    #include <string>
    #include <memory>
    #include <iostream> // only for testing...
    
    #if HAS_FUNCTION
    typedef std::function<void(std::string)> function_type;
    #else
    class function_type
    {
    private:
        struct base {
            virtual ~base() {}
            virtual base* clone() const = 0;
            virtual void  call(std::string const&) = 0;
        };
        template <typename Function>
        struct concrete
            : base {
            Function d_function;
            concrete(Function function)
                : d_function(function) {
            }
            base* clone() const { return new concrete<Function>(this->d_function); }
            void  call(std::string const& value) { this->d_function(value); }
        };
        std::auto_ptr<base> d_function;
    public:
        template <typename Function>
        function_type(Function function)
            : d_function(new concrete<Function>(function)) {
        }
        function_type(function_type const& other)
            : d_function(other.d_function->clone()) {
        }
        function_type& operator= (function_type other) {
            this->swap(other);
            return *this;
        }
        ~function_type() {}
        void swap(function_type& other) {
            std::swap(this->d_function, other.d_function);
        }
        void operator()(std::string const& value) {
            this->d_function->call(value);
        }
    };
    #endif
    
    class functionbuf
        : public std::streambuf {
    private:
        typedef std::streambuf::traits_type traits_type;
        function_type d_function;
        char          d_buffer[1024];
        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()? traits_type::not_eof(c): traits_type::eof();
        }
        int sync() {
            if (this->pbase() != this->pptr()) {
                this->d_function(std::string(this->pbase(), this->pptr()));
                this->setp(this->pbase(), this->epptr());
            }
            return 0;
        }
    public:
        functionbuf(function_type const& function)
            : d_function(function) {
            this->setp(this->d_buffer, this->d_buffer + sizeof(this->d_buffer) - 1);
        }
    };
    
    class ofunctionstream
        : private virtual functionbuf
        , public std::ostream {
    public:
        ofunctionstream(function_type const& function)
            : functionbuf(function)
            , std::ostream(static_cast<std::streambuf*>(this)) {
            this->flags(std::ios_base::unitbuf);
        }
    };
    
    void some_function(std::string const& value) {
        std::cout << "some_function(" << value << ")\n";
    }
    
    int main() {
        ofunctionstream out(&some_function);
        out << "hello" << ',' << " world: " << 42 << "\n";
        out << std::nounitbuf << "not" << " as " << "many" << " calls\n" << std::flush;
    }
    

    A fair chunk of the above code is actually unrelated to the task at hand: it implements a primitive version of std::function<void(std::string)> in case C++2011 can’t be used.

    If you don’t want quite as many calls, you can turn off std::ios_base::unitbuf and only sent the data upon flushing the stream, e.g. using std::flush (yes, I know about std::endl but it unfortunately is typically misused to I strongly recommend to get rid of it and use std::flush where a flush is really meant).

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

Sidebar

Related Questions

I have a simple Java program that reads in a text file, splits it
I have a Windows program that has a GUI which also uses a command
I need to write a simple terminal-based program that should, Read some text from
I have a simple GUI program that creates a new window (which contains a
I have a GUI application that uses an InputVerifier to check the content of
I have an old program that uses XUL and Mozilla Prism to display a
I have the unfortunate task of maintaining an orphaned Perl program that uses Perl/TK
I have a simple console app that clients will install on their server which
I have a GUI program that, in this example, downloads an exe using wget
I am using vb.net 2010 and I have created a program that uses sockets

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.