Skip to content
Advertisement

Redirecting stdout and stderr to a file in c++

I am trying to redirect stdout and stderr to a log file.

JavaScript

This is what i am doing. But still i am missing out some of the logs. I came to know that when i executed my application commenting out these lines of code. I am dubious about this code snippet. Please provide your feedback on this.

Advertisement

Answer

first close stdout and stderr

JavaScript

open new file to write logs to.

JavaScript

duplicate the logfile file descriptor to use with stdout and stderr. see man dup2

JavaScript

I hope above code help you..

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement