Acaccia wrote in
It is possible to redirect stdout on a std::string instead of a file?
#include <iostream>
#include <ostream>
#include <sstream>
int main()
{
std:
stringstream oss;
std::cout.rdbuf( oss.rdbuf() );
std::cout << "here's some text";
std::cerr << "[" << oss.str() << "]\n";
}
How? Alternatively, how can I print stdout of an application to a
QWidget (QTextEdit) with linux KDE?
You should start by creating a custom streambuf:
#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::cerr << "\n";
}
I belive (*) the above is all you *have* to do to create a
custom output streambuf, but you it might be worth while
to override xsputn and/or to provide a single line buffer.
If you do add a buffer to the above you should also override
sync().
(*) If I am wrong somebody *will* correct me, so wait and see
.
Rob.