Originally Posted by qwerty4061
I am having some confusion regarding the device file /dev/stdout (and stdin). I am not understanding the purpose of having a file like this. As I understand stdout and stdin are redirected to some device like /dev/tty or a serial port. So what purpose is served by having 2 extra device files.
ls > /dev/stdout and ls >/dev/tty gives the same output.
Also if I do something like this
fgets(str, 80, stdin); inside a code, what does stdin mean here.
I'll admit it is a little vague to me, but as I understand it: /dev/stdout actually points to /proc/self/fd/1 (at least on my system), so it points to the standard out stream for your process, whatever that happens to be. Whereas, I believe, /dev/tty points to the controlling terminal for the process. Maybe this illustration code helps:
$ cat testout.sh
echo "hi there" >> /dev/tty
$ bash testout.sh >> testout
$ cat testout
(nothing is printed)
So here, I have a script that sends "hi there" to the controlling terminal. Then, I run the script, but I direct the standard output of the script to a file called "testout". The words "hi there" are in fact printed to my controlling terminal, but nothing is sent to the file "testout". So, I believe the answer is that standard out and your controlling terminal don't have to be the same. (Somebody out there correct me if I have been technically inaccurate...)
To your second question, "stdin" is a FILE stream defined in "stdio.h". From STDIN(3):
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,
The files /dev/stdout
, and /dev/stderr
I believe are provided simply for convenience. In programming you'll just use functions like fgets, printf, and so forth.