39517

Getting stdout contents without disabling cout/cin

Question:

Is it possible to save terminal (previously) written contents (stdout), to an array of strings:

vector<string> lines;

without disabling cin/cout normal functionality, in a linux console application.

In a terminal window in addition to characters there's coloring information for each character. Is there a way to save them in another array?

Answer1:

If you mean you want to write inside a program to std::cout and have the data appear both on the terminal and in some program-internal data structure, you would use a custom stream buffer. Here us a simple example of how this would look:

#include <streambuf> #include <iostream> struct capturebuf : std::streambuf { std::ostream& d_out; std::streambuf* d_sbuf; std::string& d_str; public: capturebuf(std::ostream& out, std::string& str) : d_out(out) , d_sbuf(out.rdbuf()) , d_str(str) { out.rdbuf(this); } ~capturebuf() { this->d_out.rdbuf(this->d_sbuf); } int overflow(int c) { if (c != std::char_traits<char>::eof()) { this->d_str.push_back(c); } return this->d_sbuf->sputc(c); } int sync() { return this->d_sbuf->pubsync(); } };

The above stream buffer will just add each character written to the string referenced during construction. The characters are also forwarded to the passed stream buffer. The forwarding to pubsync() is needed to make sure the underlying stream is flushed at the appropriate times.

To actually use it, you need to install it into std::cout. Since the constructor and destructor of capturebuf already do the registration/deregistration, all it takes is to construct a corresponding object:

int main() { std::string capture; { capturebuf buf(std::cout, capture); std::cout << "hello, world\n"; } std::cout << "caught '" << capture << "'\n"; }

This code doesn't quite do what you asked but nearly: you'd just need to process newlines differently. What is entirely missing is buffering which isn't needed to get the functionality but to get the performance. The above was typed in on a mobile device and is provably riddled with typos but the approach should work as is.

Recommend

  • C++ String tokenisation from 3D .obj files
  • Determining the length of a read stream in node js
  • Using HTML/CSS for UI in XNA?
  • incomplete type 'struct' error in C
  • C function strchr - How to calculate the position of the character?
  • Unzip archive in .Net CORE 1.0
  • Streaming screenshots over WebRTC as a video stream from iOS
  • Sensibility of combined Maven/Ant+Ivy build management for dual platform Desktop/Android deployment?
  • aapt.exe'' finished with non-zero exit value 1
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • Trying to get the char code of ENTER key
  • Android app gives error “BatteryStatsImpl: reading network stats”
  • Using a canvas object in a thread to do simple animations - Java
  • Unable to install Git-core+svn by MacPorts
  • Django simple Captcha “No module named fields” error
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Play WS (2.2.1): post/put large request
  • Bad request using file_get_contents for PUT request in PHP
  • Could not find rake using whenever rails
  • NHibernate Validation Localization with S#arp Architecture
  • SignalR .NET Client Invoke throws an exception
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Bug in WPF DataGrid
  • How to add date and time under each post in guestbook in google app engine
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Redux, normalised entities and lodash merge
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Web-crawler for facebook in python
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • trying to dynamically update Highchart column chart but series undefined
  • coudnt use logback because of log4j
  • how does django model after text[] in postgresql [duplicate]
  • embed rChart in Markdown
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app