stdout redirection (2)

Joined
Aug 26, 2009
Messages
1
Reaction score
0
Hi there,

I found this piece of code to redirect the stdout, but unfortunately I get a segment fault at the end of the program :(. Is there a way to get rid of that, I guess the error is in the destructor of the streambuf, but have no idea of that stuff...

Otherwise I take also another code, I simply need my gui to take the standard output and put it to a log element / so a function, which gets the chars, which I can use.

original post: ./forums/t280688-stdout-redirection.html

Code:
#include <iostream>
#include <ostream>
#include <sstream>
#include <streambuf>

class mystreambuf:
public std::basic_streambuf< char, std::char_traits< char > >
{

    typedef
    std::basic_streambuf< char, std::char_traits< char > >
    ::
    int_type
    int_type
    ;

    typedef std::char_traits< char > traits_t;


    int_type overflow( int_type c )
    {
        std::cerr << traits_t::to_char_type( c );

        // in your code write c to your QT widget hwever that
        // is done .

        return c;
    };


};

int main()
{
    mystreambuf mybuf;
    std::cout.rdbuf( &mybuf );

    std::cout << "here's some text";

    std::cout << "\n";

    std::cout.rdbuf();

    return 0;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top