8

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user40120
  • 648
  • 5
  • 28
  • 58

5 Answers5

16

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
Andrew Coleson
  • 10,100
  • 8
  • 32
  • 30
  • appending eh... seems to be what everyone is doing with my answer to theirs. +1 for your efforts though. – John T Feb 22 '09 at 08:06
  • Joel'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. – Andrew Coleson Feb 22 '09 at 08:12
  • You should probably also mention stderr redirection (with "2>x.txt" or "2>&1" if I remember correctly.) – aib Feb 23 '09 at 01:53
  • Wasn't going to get that specific, but I guess it's worth aggregating for future searches (and giving appropriate credit to John and Brian). – Andrew Coleson Feb 23 '09 at 16:08
9

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
Community
  • 1
  • 1
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
6

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.

John T
  • 23,735
  • 11
  • 56
  • 82
3

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.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
3
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.

shoosh
  • 76,898
  • 55
  • 205
  • 325