3 ms·
Standard output for errors is for errors. If you need one more stream with your own rules, just open it. #include <stdio.h> int main(void) { /* Open th
by v_lisivka 8y ago
Standard output for errors is for errors. If you need one more stream with your own rules, just open it.
#include <stdio.h>
int main(void) {
/* Open third stream. Stream must be opened in shell using 3>... */
FILE *user = fdopen (3, "w");
/* If third stream is not open, then print to first stream. */
if (!user) user = fdopen(1, "w");
printf("Output.\n");
fprintf(stderr, "Error!\n");
fprintf(user, "Important message to user!\n");
fclose(user);
return 0;
}
$ gcc test.c -o test
$ ./test
Output.
Error!
Important message to user!
$ ./test 1>out.txt 2>error.log 3>/dev/tty
Important message to user!
- dickeytk 8y agoWell first of all the docs say it's for "diagnostic output" http://pubs.opengroup.org/onlinepubs/9699919799/functions/stderr.html http://pubs.opengroup.org/onlinepubs/9699919799/functions/st... not "errors". Secondly, it doesn't matter what's right or not, the reality is CLIs (that you didn't write) generally use stderr for progress so you can't assume everything on it is an error.
- v_lisivka 8y agoGenerally, CLI turns out progress and colors when output is not connected to tty, so I can safely assume, in most cases. that everything on stderr is about errors. If not, I can always send my patch to upstream.
- v_lisivka 8y agoActually, Linux manual says: DESCRIPTION Under normal circumstances every UNIX program has three streams opened for it when it starts up, one for input, one for output, and one for printing diagnostic or error messages. These are typically attached to the user's terminal (see tty(4)) but might instead refer to files or other devices, depending on what the parent process chose to set up. (See also the "Redirection" section of sh(1).) The input stream is referred to as "standard input"; the output stream is referred to as "standard output"; and the error stream is referred to as "standard error". These terms are abbreviated to form the symbols used to refer to these files, namely stdin, stdout, and stderr.
- dickeytk 8y agoRight, as I said: stderr is not only for errors