freopen_s option

Hello. I made a little program which works in the windows console. All works as expected, but I would like to save some data in a txt file. So I use what I found on the web - freopen_s function. I have a data file as expected, but now I see nothing in the console. I would like to get all output in the console, and I want a txt file with the same data. What am I supposed to do? Thank you for your help ++

 
  freopen_s(&stream, "data.txt", "w+", stdout);

Using this function, I have my file with data, but nothing in the console.
Last edited on
The function redirects output from the console to the specified file. You won't see any output in the console after invoking the function.
if you want both, you are going to have to echo it.
eg
cin >> string
fileout << string
and
cout << text
fileout << text
Last edited on
If you're on Unix/Linux, or the work-alikes like Msys/Cygwin/WSL, perhaps
https://www.man7.org/linux/man-pages/man1/tee.1.html
Thank you for your replies. Creating an echo for data seems to me not really efficient. I guess that there is a buffer so as to manage output in the console. I would like to wait until the end of the process - and then, save what has been written. Is there a way ?
If you do not mind waiting for the end of the program, you can use a system call to cat or type (unix, windows) and just print the text file to the console.

you can also buffer up your output and print it at the end or periodically (if large) by just dumping it into a string until you feel like doing a print. or maybe a stringstream would work, since it can replace iostream with a string like tool.. easier to modify the code maybe?

no matter what you do, printing into a console window is one of the slowest operations you can do (its buffered already if you do not use endl to flush the buffer or .flush() etc) but its still sluggish.
you don't notice this on normal small homework sized problems but big data dumps take a long, long time. This isn't anything you can fix; the best fix is to dump to a file and let the user handle the output their own way if you want to blast through the processing. The only catch to doing that is you can't prompt for inputs if the user has no cues.
Last edited on
Creating an echo for data seems to me not really efficient.
It is "more efficient" than redirecting all console output to a file; you have no clue what the program is doing until it finishes and you inspect the output file.

I guess that there is a buffer so as to manage output in the console.
Tapping into the buffer efficiently so you can simultaneously output to the console and a file is likely to be more complicated, as well as prone to unforeseen errors, than simply echoing the output.

C++ goes the I/O stream route to make input and output as seamlessly generic as possible no matter where the data comes from or goes to.
Ok. Thank you for all your explanations which are really useful. Finally, creating an echo for all data could be the best solution. I will try. Thank you everyone ++
Last edited on
It is "more efficient" than redirecting all console output to a file; you have no clue what the program is doing until it finishes and you inspect the output file.

yea, but benchmark something that produces a lot of output when os redirected vs console. The difference is eye opening. eg 'program > filename' instead of program... once it is debugged, of course! A really easy way to see it is (windows) dir/s from c:
Last edited on
Registered users can post here. Sign in or register to post.