40

I'm writing a C program in Windows, my printf calls print to the command line, and I know that I can redirect all this output to a text file using:

myProgram.exe > mylog.txt

However, I wish to also see the output that would have been printed to the console as well as log all of it in a text file.

Is there a way to do this? I was thinking of using tail to monitor the log file.

studiohack
  • 13,477
user79397
  • 503

4 Answers4

27

The windows PowerShell has a tool that can do that, named tee after the unix tool which does the same.

Alternatively, there are ports of the unix tee for windows:

Lesmana
  • 20,621
15

Under Windows all I can think is to do this:

myProgram.exe > mylog.txt & type mylog.txt

This is based on the command example in your question - if in fact you wanted to append the output to mylog.txt then you'd want to use >> instead of >, but type would print out the entire log file, not just what had been appended.

If you download the GnuWin32 CoreUtils, you can use the Unix method (tee command) for doing this:

myProgram.exe | tee mylog.txt

This will write the output of myProgram.exe to mylog.txt but also display it to the console at the same time. If you only want to append to mylog.txt then you can pass the -a parameter to tee.

Gareth
  • 19,080
1

I use Visual Studio Code and open the log file from there, it keeps the view up to date in near real-time as the log file changes

0

I just had a similar need and used Tail as the OP suggested they might:

>C:\Temp\Commands_Log.txt (
START tail.exe -f C:\Temp\Commands_Log.txt

Some_Commands
Other_Commands

echo.
echo ALL DONE HERE!
echo.
echo IT IS NOW SAFE TO CLOSE THIS WINDOW!
)

The ">C:\Temp\Commands_Log.txt" creates the log file and adds the output from the all the commands located inside the (parentheses).

The first command inside the parentheses should be to start the Tail, which will open in a new command window.

The echo's at the end are for unfamiliar users to let them know when everything is complete.

DBADon
  • 503