Writing ALL program output to a txt file in C++
Solution 1
This is a duplicate of: this question
You can redirect stdout, stderr and stdin using std::freopen
.
From the above link:
/* freopen example: redirecting stdout */
#include <stdio.h>
int main ()
{
freopen ("myfile.txt","w",stdout);
printf ("This sentence is redirected to a file.");
fclose (stdout);
return 0;
}
You can also run your program via command prompt like so:
a.exe > stdout.txt 2> stderr.txt
Solution 2
If your program already uses cout/printf and you want to send EVERYTHING you currently output to a file, you could simply redirect stdout to point to a file before your existing calls: http://support.microsoft.com/kb/58667
Relevant Code:
freopen( "file.txt", "w", stdout );
cout << "hello file world\n"; // goes to file.txt
freopen("CON", "w", stdout);
printf("Hello again, console\n"); // redirected back to the console
Alternatively if you just want Some things to be printed to a file, you just want a regular file output stream: http://www.cplusplus.com/doc/tutorial/files.html
Relevant Code:
ofstream myfile;
myfile.open("file.txt");
myfile << "Hello file world.\n";
printf("Hello console.\n");
myfile.close();
EDIT to aggregate answers from John T and Brian Bondy:
Finally, if you're running it from the commandline, you can just redirect the output as everyone else mentioned by using the redirect operator ">" or append ">>":
myProg > stdout.txt 2> stderr.txt
Solution 3
If you want all output in a text file you don't need to code anything extra.
from the command line:
program > output.txt
If you only wish to redirect certain output you can use ostream as dirkgently suggested.
Solution 4
Then you cannot use std::cout anywhere else to print stuff from your program. Change std::cout to a std::ostream and then pass your file or std::cout as required.
Solution 5
sOutFile << stdout;
in C "stdout
" is defined as a FILE*
variable. It's just a pointer. Outputing it to a file just writes the value of the pointer, in your case: 0x77c5fca0
to the file.
If you want to direct your output to a file either write it the a file in the first place or redirect the output of your program to a file using the command line.
user40120
Updated on July 23, 2022Comments
-
user40120 5 months
I need to write all my program output to a text file. I believe it's done this way,
sOutFile << stdout;
where sOutFile is the ofstream object that creates the file like this:
sOutFile("CreateAFile.txt" ); // CreateAFile.txt is created.
When I insert the stdout into the sOutFile object, I get some code which seems to resemble
octal[hexadecimal] code or an address of some kind in the text file that I created.0x77c5fca0
But what's confusing to me is that in my program I use cout several times. Mostly just literal statement. If I'm not mistaken that is the program output.
If this code is an address, would it contain all of my output? Could I read it back in to the program and find out that way?
What can I do to get ALL of my program output written to a text file?
-
John T almost 14 yearsappending eh... seems to be what everyone is doing with my answer to theirs. +1 for your efforts though.
-
oneAday almost 14 yearsJoel's said that a good thing to do is to go back to a question and combine all the good answers..it's a dirty job, but somebody's got to do it. And it's silly to leave an answer incomplete as other people provide suggestions you don't think of, so I try to vote you up and add it to mine too.
-
aib almost 14 yearsYou should probably also mention stderr redirection (with "2>x.txt" or "2>&1" if I remember correctly.)
-
oneAday almost 14 yearsWasn't going to get that specific, but I guess it's worth aggregating for future searches (and giving appropriate credit to John and Brian).