cout into another/new terminal

Hi all, I want to cout some text in another terminal. Reason is the program I am working on, when it is running, will give many error warnings although it is harmless. But these warning fill up the whole screen too quickly and I cant see my text.

std::cout<<"some text"<<std::endl;

I want the "some text" to appear in another terminal while the warnings will appear in the terminal which I called my program from. I am using Ubuntu btw!

Thanks for the help!
Last edited on
You can send them to a text file so when you run your program just do:

./myprog 2> somefile

This will send all error messages to the file you specified. Or you can do:

./myprog > somefile

This will send all standard output to the file
> I want the "some text" to appear in another terminal
> while the warnings will appear in the terminal which I called my program from.

C++ has three standard output streams (char):
std::cout (stdout), std::cerr (stderr) and std::clog (stderr, buffered).

A typical program would send normal program output to std::cout, error messages to std::cerr and log messages to std::clog

As Smac89 pointed out, stdout and stderr can be redirected from the shell.

Each of these streams can also be redirected from within the program. For example,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>

int main()
{
    // redirect std::cerr and std::clog output to files

    std::filebuf errbuf ;
    errbuf.open( "errors.txt", std::ios::out ) ;
    std::streambuf* old_cerr_buf = std::cerr.rdbuf( &errbuf ) ;

    std::filebuf logbuf ;
    logbuf.open( "logfile.txt", std::ios::out|std::ios::app ) ;
    std::streambuf* old_clog_buf = std::clog.rdbuf( &logbuf ) ;


    // ....

    std::cout << "normal output\n" ;
    std::cerr << "error message\n" ;
    std::clog << "log message\n" ;

    // ....

    // restore default for std::cerr and std::clog

    std::cerr.rdbuf( old_cerr_buf ) ;

    std::clog << std::flush ;
    std::clog.rdbuf( old_clog_buf ) ;

    std::cout << "normal output 2\n" ;
    std::cerr << "error message 2\n" ;
    std::clog << "log message 2\n" ;
}

Topic archived. No new replies allowed.