0

Suppose my whole screen (console) is filled with some text (e.g. printed through std::cout). How can I get some characters of this console output, e.g. in the center of screen?

In Windows I can get characters of console through ReadConsoleOutputCharacter function.

But how to do same thing in Linux (inside C++ (or C) code)?

Is there any easy way to do this without using extra huge libraries like ncurses? Maybe there are some standard Linux kernel sys-call functions to achieve this task?

I need to get console text of current running program. I.e. if inside my program I printed some text, later inside this same program I want to read parts of this printed to console text.

Arty
  • 14,883
  • 6
  • 36
  • 69
  • 1
    Neither C nor C++ have that capability as part of the language and standard library. You'd need to do it with an extra huge library like `ncurses`. – Eljay Dec 03 '20 at 13:09
  • 4
    Please try to avoid the term "C/C++". There is no such languages as "C/C++", only the two separate and *very* different languages C and C++. – Some programmer dude Dec 03 '20 at 13:09
  • @Eljay Maybe there is some built-in OS system call function for doing this, i.e. some syscall implemented in Linux kernel? – Arty Dec 03 '20 at 13:10
  • This might answer your question https://stackoverflow.com/questions/49430983/reading-from-console-output-in-c – 66Gramms Dec 03 '20 at 13:11
  • You will need to hook into terminal program, such as Gnome Terminal and obtain screen buffer content somehow. ncurses or VT100 won't help you at all. – user7860670 Dec 03 '20 at 13:16
  • @user7860670 I forgot to mention that I need to get console text of current running program, if it helps to solve task, i.e. if I printed in my C++ program some text, later in inside same program I want to get this text back at some position of console. – Arty Dec 03 '20 at 13:18
  • Then you can implement your own screen buffer and print everything through it. This is basically what ncurses does with addch + inchnstr. However note that the content that end user sees on the screen will not necessary match content of this internal buffer. – user7860670 Dec 03 '20 at 13:21
  • @user7860670 Is there a standard way to read any type of current console buffer? Not to make code specific only to Gnome Terminal. Any code that is generic for all kinds of terminals. – Arty Dec 03 '20 at 15:16
  • First of all there could be no current console buffer. Output may go into file or printed. On Windows they provide a nice API to work with stock console output program or its substitute, but on linux there are no such things as nice API or stock programs. So i guess each specific environment and console emulator will require specific solution. – user7860670 Dec 03 '20 at 15:25

0 Answers0