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.
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.
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: