Jump to content

I've made a C++ program that does its job well, and I'm quite satisfied with the result, since I'm not that familiar with the language yet. It's a console application. Now here's my question: is there a nice and simple way for "dumping" all the contents of the console into a text file while the program runs?

Link to comment
https://linustechtips.com/topic/910132-c-log-file/
Share on other sites

Link to post
Share on other sites

The easiest way (from outside the program):
MyProgram > logfile.txt 2>&1

This will redirect all stdout (and stderr) to the file logfile.txt. If logfile.txt already exists, it will be overwritten by this command, so be careful. If you instead want to append the output to the file, use: 

MyProgram >> logfile.txt 2>&1

The 2>&1 at the end tells the shell to close stderr and instead redirect it to stdout, so that the file will get stdout and stderr. If you don't care about stderr, you can omit this part.

Link to comment
https://linustechtips.com/topic/910132-c-log-file/#findComment-11180270
Share on other sites

Link to post
Share on other sites

22 minutes ago, akio123008 said:

From outside the program? do you mean in a separate command prompt?

When you launch the program, you can redirect standard output to a file as in the manner described.

 

However, this may override standard output entirely, meaning you won't see anything show up on the console.

 

EDIT: If you want the program to output to a file without mucking with command line options (because not everyone is versed enough to know that), I would suggest using the I/O library: http://www.cplusplus.com/doc/tutorial/files/

Link to comment
https://linustechtips.com/topic/910132-c-log-file/#findComment-11180380
Share on other sites

Link to post
Share on other sites

20 minutes ago, M.Yurizaki said:

EDIT: If you want the program to output to a file without mucking with command line options (because not everyone is versed enough to know that), I would suggest using the I/O library: http://www.cplusplus.com/doc/tutorial/files/

Well that's a nice solution. Thank you.

Link to comment
https://linustechtips.com/topic/910132-c-log-file/#findComment-11180437
Share on other sites

Link to post
Share on other sites

2 minutes ago, akio123008 said:

Well that's a nice solution. Thank you.

As a bonus tip, if you need to log data in a table form, I highly suggest using the comma-separated value format (which saves as a .csv file). It's plain text and practically all spreadsheet programs can open it up.

Link to comment
https://linustechtips.com/topic/910132-c-log-file/#findComment-11180451
Share on other sites

Link to post
Share on other sites

4 hours ago, akio123008 said:

I've made a C++ program that does its job well, and I'm quite satisfied with the result, since I'm not that familiar with the language yet. It's a console application. Now here's my question: is there a nice and simple way for "dumping" all the contents of the console into a text file while the program runs?

If you've written the program using iostreams (std::cout) then it should be rather easy to add this functionality.

All your output operations should accept any std::ostream. So you can make a std::ostream reference and write to that rather then std::cout. Then you can assign any output stream to this reference you want, be it std::cout, a std::ofstream or some std::ostream derivative you've yet to think of and create.

Link to comment
https://linustechtips.com/topic/910132-c-log-file/#findComment-11181089
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×