Saving console output to .txt/.html file using C++/VC++
-
Hi, I am using CPPUNIT code for Unit testing purpose. I have created a Win32-console application to show its output. Now i want to generated TEXT/HTML/ formated file which will contain the excat lines{output} as in console o\p window. Please suggest what should be VC++ code to achive this requirement. Thanks
-
Hi, I am using CPPUNIT code for Unit testing purpose. I have created a Win32-console application to show its output. Now i want to generated TEXT/HTML/ formated file which will contain the excat lines{output} as in console o\p window. Please suggest what should be VC++ code to achive this requirement. Thanks
-
Hi, I am using CPPUNIT code for Unit testing purpose. I have created a Win32-console application to show its output. Now i want to generated TEXT/HTML/ formated file which will contain the excat lines{output} as in console o\p window. Please suggest what should be VC++ code to achive this requirement. Thanks
You can solve this in two different ways: Inside your program or outside your program. The "outside" solution is probably simpler and more roboust and easier to implement. Outside: When you are running your program you redirect its output to another program that does the following: prints the incoming data to the console but at the same time it writes it into a text file and of course at the same time it can do other things too with the log data. You can do the redirection either by writing another program that runs the test with redirected input/output or you can choose an easier solution, piping together programs with the help of your cmd or bash: Example python script that prints the input to console and logs to file at the same time: file: x.py
import sys
with open('logfile.txt', 'w') as log_file:
while 1:
s = sys.stdin.readline()
if not s:
break
sys.stdout.write(s)
log_file.write(s)
log_file.flush()Commandline usage:
TestProgram | python x.py
Of course this program could be anything, not only a python script but a perl script or a c/C++ program or a java program. Inside: If you want to solve the problem inside your test program then create your own logger and log through that instead of using printf() or similar methods. In your logger you can create many outputs so when your test program logs something your logger can direct it towards different outputs for example a console writer output, a html writer output and a text file writer output. Almost forgot to mention: If you are on linux or you have cygwin on windows then you can use the tee command that does basically the same as the above python script.