How to read the console output?

Hello,

i would like to know if is possible to read (and eventually edit) the console output and store it a buffer.

I'm using windows and gcc.

Thank you very much.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <sstream>
#include <cctype>

int main()
{
    std::stringbuf sbuf( std::ios::out ) ; // create a stringbuf
    auto oldbuf = std::cout.rdbuf( std::addressof(sbuf) ) ; // associate the stringbuf with std::cout

    std::cout << "hello " << 123 << '\t' << std::fixed << std::showpos << 1235678.9 << " bye!\n\n" ; // use std::cout normally

    std::cout.rdbuf( oldbuf ) ; // restore cout's original buffer

    std::string output = sbuf.str() ; // get a copy of the underlying string
    for( char& c : output ) c = std::toupper(c) ; // modify it
    
    std::cout << output ; // print out the modified output
}

http://coliru.stacked-crooked.com/a/69627b622f317b29
Thank you.

I will try
Topic archived. No new replies allowed.