stdout redirection

A

Acaccia

It is possible to redirect stdout on a std::string instead of a file? How?
Alternatively, how can I print stdout of an application to a QWidget
(QTextEdit) with linux KDE?
Thank's in advance!
 
V

Victor Bazarov

Acaccia said:
It is possible to redirect stdout on a std::string instead of a file? How?

Use std::eek:stringstream instead of std::cout.
Alternatively, how can I print stdout of an application to a QWidget
(QTextEdit) with linux KDE?

Ask in a Linux newsgroup.

Victor
 
R

Rob Williscroft

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::eek: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.
 
A

Acaccia

Thank's a lot! :eek:)

Rob Williscroft said:
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::eek: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.
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top