14

I'm new to c. Is there any simple way to redirect all the console's output (printfs etc.) to a file using some general command line \ linkage parameter (without having to modify any of the original code)?

If so what is the procedure?

divanov
  • 6,173
  • 3
  • 32
  • 51
vondip
  • 13,809
  • 27
  • 100
  • 156
  • 3
    [Yes of course there is](http://www.tutorialspoint.com/unix/unix-io-redirections.htm) – r3mainer Nov 22 '13 at 22:34
  • Possible duplicate of [Redirect stdout and stderr to a single file](http://stackoverflow.com/questions/1420965/redirect-stdout-and-stderr-to-a-single-file) – Jonathan Benn Feb 04 '16 at 20:55

4 Answers4

37

Use shell output redirection

your-command > outputfile.txt

The standard error will still be output to the console. If you don't want that, use:

your-command > outputfile.txt 2>&1

or

your-command &> outputfile.txt

You should also look into the tee utility, which can make it redirect to two places at once.

Hut8
  • 6,080
  • 4
  • 42
  • 59
3

On unices, you can also do:

your-command | tee output file.txt

That way you'll see the output and be able to interact with the program, while getting a hardcopy of the standard output (but not standard input, so it's not like a teletype session).

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • while ssh to a server using this command can save whole session file. Awesome!! –  Nov 05 '14 at 13:33
3

As mentioned above, you can use the > operator to redirect the output of your program to a file as in:

./program > out_file

Also, you can append data to an existing file (or create it if it doesnt exit already by using >> operator:

./program >> out_file

If you really want to learn more about the (awesome) features that the command line has to offer I would really recommend reading this book (and doing lots of programming :))

http://linuxcommand.org/

Enjoy!

Andrés AG
  • 381
  • 1
  • 4
  • 12
2

In Unix shells you can usually do executable > file 2> &1, whch means "redirect standard output to file and error output to standard output"

gpeche
  • 21,974
  • 5
  • 38
  • 51