2

Hello I am writing a program in C. I start it like that after compilation.

/a.out <source.txt >output

I want all messages to be printed to the output.txt. However I would like to send errors to the console, not to the file. The problem is when i use this statement in my code:

freopen( "errors.txt", "w", stderr );
fprintf (stderr, "%s\n", s);

All Erros are also printed to the output.txt file not to errors.txt I would be grateful for any help.

rici
  • 234,347
  • 28
  • 237
  • 341
Dago
  • 788
  • 2
  • 9
  • 24
  • 1
    How exactly did you invoke your executable with output redirection? Can you paste your command line? – Hellmar Becker Jan 28 '16 at 07:46
  • Only way that this command is printed into output.txt is that you've redirected your stderr to some file. – Aleksandar Makragić Jan 28 '16 at 07:47
  • Possible duplicate of [In C how do you redirect stdin/stdout/stderr to files when making an execvp() or similar call?](http://stackoverflow.com/questions/14543443/in-c-how-do-you-redirect-stdin-stdout-stderr-to-files-when-making-an-execvp-or) – Aleksandar Makragić Jan 28 '16 at 07:49
  • 1
    Possible duplicate of [How to redirect both stdout and stderr to a file](http://stackoverflow.com/questions/7526971/how-to-redirect-both-stdout-and-stderr-to-a-file) – Pierre Jan 28 '16 at 07:58
  • Your question is contradictory: 1st you write " I would like to send errors to the _console_", and below you write that errors are also printed to output.txt and not to _errors.txt_. Please clarify. And also `/a.out output` wont write to `output.txt` but to `output`. – Jabberwocky Jan 28 '16 at 08:02
  • thank you for help. I just wanted to send my stdderr to different place than stdout. The answer below helped me. – Dago Jan 28 '16 at 08:31

1 Answers1

1

By default the stdout and stderr are together routed to your console. However you can redirect the error stream to a file with the help of the following form of your command line: ./a.out 2>logfile.log. In that case the stdout will still come to the console, but stderr will go to the file.

dmi
  • 1,424
  • 1
  • 9
  • 9