Reputation:
Is the standard input file- stdin
is always the keyboard, the standard output file- stdout
is always the screen, and the standard error file- stderr
is always the screen?
and why?
Upvotes: 2
Views: 1328
Reputation: 3274
By default, yes. But the reason that system is so flexible and powerful is that it can be redirected (by the user or the program both)
When you type in a shell
command > file
You actually redirect the stdout of command
into the file file
.
By doing
command1 | command2
you redirect the stdout of command1
to the stdin of command2
Programatically, the file descriptor 0 is always stdin, 1 stdout, 2 stderr.
I suggest learning about dup
and dup2
for redirecting them programatically.
Example
int file = open("out.txt", O_APPEND | O_WRONLY);
int stdout_cpy = dup(1); // Clone stdout to a new descriptor
dup2(file, 1); // Make file the new fd 1, i.e. redirect stdout to out.txt
Upvotes: 3