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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:41:32+00:00 2026-05-12T06:41:32+00:00

I am looking for a way to branch (tee) the input read from an

  • 0

I am looking for a way to branch (tee) the input read from an istream (cin, in my case) out to a log file (clog/ofstream/etc), while still using the input for processing.

I have read about boost::tee_device, and it is very similar to my requirements. Unfortunately, it is implemented as an ostream, and thus solves a similar problem from “the other side of the pipe”.

I attempted to write an istream (adaptor) class which forwards the input functions on to a wrapped input stream (cin), and also sends what was read to the log file.

This works fine for basic types which call operator>>(…) directly, however, I have run into issues with some more advanced usage of the input stream, for example, for operator>>(std::string), and the std::string getline function.

Is there any easier way to do this (possibly via rdbuf() manipulation)?

Thanks!

Edit: I could change my code all over the place to something like: cin >> value; clog << value; — but that would be a significant and ugly change. I would also prefer to have an easy way to turn logging off. Thus, I would like a way to model this as an istream “filter” and then simply replace all references to cin with this istream “logger”.

Ideal Solution:

class log_istream : public std::istream
{
public:
    log_istream( std::istream & in , std::ostream & out );

    /* ... istream forwarding functions ... */

private:
    std::istream & in_;
    std::ostream & out_;     
};

int main() {
    log_istream logger( std::cin , std::ofstream("logfile.out") );

    logger >> value; // this implies infile >> value and logfile << value
    getline(logger,my_string); // this also implies logfile.writeline(value)
    // etc
}

etc.

  • 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-12T06:41:32+00:00Added an answer on May 12, 2026 at 6:41 am

    Final Answer:

    #ifndef TEE_ISTREAM_H_
    #define TEE_ISTREAM_H_
    
    /*****************************************************************************/
    
    #include <boost/iostreams/tee.hpp>
    #include <boost/iostreams/invert.hpp>
    #include <boost/iostreams/device/file.hpp>
    #include <boost/iostreams/filtering_stream.hpp>
    #include <boost/tr1/memory.hpp>
    #include <iostream>
    
    /*****************************************************************************/
    
    namespace bio = boost::iostreams;
    
    /*****************************************************************************/
    
    class tee_source : public bio::source {
    public:
        tee_source( std::istream & in, const std::string & filename )
            : in_(in), log_file_(filename, std::ios::app), tee_(bio::tee(log_file_), 1)
        { }
    
        std::streamsize read(char* s, std::streamsize n)
        {
            return tee_.read(in_,s,n);
        }
    
    private:
        std::istream &                               in_;
        bio::file                                    log_file_;
        bio::inverse< bio::tee_filter< bio::file > > tee_;
    };
    
    /*****************************************************************************/
    
    typedef bio::filtering_istream                tee_istream_t;
    typedef std::tr1::shared_ptr< tee_istream_t > tee_istream_ptr_t;
    
    /*****************************************************************************/
    
    inline tee_istream_ptr_t make_tee_istream( std::istream & in, const std::string & filename )
    {
        return tee_istream_ptr_t( new tee_istream_t( tee_source( in , filename ), 0 ) );
    }
    
    /*****************************************************************************/
    
    #endif
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a way to stream a PDF file from my server to
I'm looking for way to PHP to detect if a script was run from
I'm looking for a way to delete a file which is locked by another
Looking for a way of getting the path from an URL in PHP: I
Looking for a simple way to open a source php file, replace some predefined
Looking for a way to apply a background-color to all input elements, including dropdown
Is there a way to update a side branch with information from another (master
This is a branch from this question . Branched out because the original purpose
I am looking for the best and safest way to convert a git branch
I am using the jquery-ui-dialog plugin I am looking for way to refresh the

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.