Thread: "Spying" on cout

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Is this within the program itself (I'm confused, you said you couldn't modify the code)?

    Even if it's not 'tee', you can still manipulate the streams from the outside in other ways similar to pipes.

    Also, say which OS / Compiler you're using.

    I'm guessing that the teebuf thing might be compiler specific, and that you need to fiddle it to make it work for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I don't think you need to directly call overflow or sync on your class members (I'm less sure about the latter--and I don't think you even need to define a sync function unless you're buffering the output)

    Something like this might work:
    Code:
    #include <iostream>
    #include <fstream>
    #include <streambuf>
    
    template <class charT, class destbuf_type = std::streambuf, class srcbuf_type = std::streambuf, class traits = std::char_traits<charT> >
    class copy_outbuf: public std::basic_streambuf<charT, traits>
    {
    public:
      copy_outbuf(srcbuf_type* src, destbuf_type* dest):m_srcBuf(src),m_destBuf(dest){}
    protected:
      virtual int_type overflow(int_type c)
      {
        if (!traits::eq_int_type(c,traits::eof()))
        {
          if (m_srcBuf->sputc(c) == traits::eof()) //error occurred
            return traits::eof();
          if (m_destBuf->sputc(c) == traits::eof())
            return traits::eof();
        }
        return traits::not_eof(c);
      }
      virtual std::streamsize xsputn(const charT* s, std::streamsize num)
      {
        std::streamsize written = m_srcBuf->sputn(s,num);
        m_destBuf->sputn(s,written);
        return written;
      }
    private:
      srcbuf_type* m_srcBuf;
      destbuf_type* m_destBuf;
    };
    
    int main()
    {
      std::ofstream outfile("test.txt");
      copy_outbuf<char> new_buf(std::cout.rdbuf(),outfile.rdbuf());
      std::cout.rdbuf(&new_buf);
      std::cout<<"Hello World!";
      std::cout<<'h';
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class methods to cout stream
    By shintaro in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2008, 07:27 PM
  2. cout
    By RenderedAwake in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2005, 07:14 AM
  3. How Does cout Work
    By jrahhali in forum C++ Programming
    Replies: 8
    Last Post: 08-25-2004, 03:19 PM
  4. cout vs std::cout
    By none in forum C++ Programming
    Replies: 10
    Last Post: 07-26-2004, 11:20 PM
  5. FAQ cout
    By evilmonkey in forum FAQ Board
    Replies: 1
    Last Post: 10-07-2001, 11:32 AM