user40120
user40120

Reputation: 648

Writing ALL program output to a txt file in C++

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?

Upvotes: 8

Views: 45099

Answers (5)

Brian R. Bondy
Brian R. Bondy

Reputation: 347466

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

Upvotes: 9

Andrew Coleson
Andrew Coleson

Reputation: 10320

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

Upvotes: 16

user36457
user36457

Reputation:

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.

Upvotes: 6

shoosh
shoosh

Reputation: 79013

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.

Upvotes: 3

dirkgently
dirkgently

Reputation: 111250

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.

Upvotes: 3

Related Questions