Writing to multiple output streams

On this page you will find some hints and tricks how to simplify printing the same thing to both the terminal window and a file. By adding to the code samples provided, you can create something that will work for you the best. Some of the ideas described in the following code files:

  • In Linux, everything is considered a file (folders, pipes, physical devices – e.g. optical drive etc.). Even writing text to the terminal window is considered writing to a file [ Wikipedia: everything is a file ]. The three standard streams are stdin (input stream from keyboard), stdout (output stream to the terminal) and stderr (error stream, also written to the terminal by default).
    Additional reading: file descriptors [ Wikipedia: file descriptor ]
  • By using the properties of the files / streams, we can investigating printing and scanning:
This means that we can use the same functions we use to operate with files to operate with the terminal window. We only need to specify the stream as stdin or stdout to, depending on the action.

  • In addition to this, if we check for the value of the file pointer (not being NULL), we can make it so that log files are only written when opening of the file succeeded or make it switchable by user depending on their wishes – e.g. we can have a debug mode where it logs everything, but in normal mode it only prints on the screen.
  • When going even deeper, we can add timestamps to our logfiles. More information and examples on this can be found from here:  http://www.cplusplus.com/reference/ctime/