First you know the difference between FILE *stin and Stdin_fileno.
stdin type is file*
Stdin_fileno type is int
The functions that use stdin are mainly: Fread, fwrite, fclose, and so on, basically beginning with F
Functions that use Stdin_fileno are: Read, write, close, etc.
The file APIs provided at the operating system level represent files in file descriptors. Stdin_fileno is the file descriptor for the standard input device (typically the keyboard).
Standard C + + at the level of the file Operations function libraries are used file* to represent the file, stdin is to point to the standard input device file file*.
As follows:
Reference to stdin (3) Man manual: Name
stdin, stdout, stderr -standard I/O streams synopsis
#include <stdio.h>
extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;... On program startup, the integer file descriptors associated with the streamsstdin,stdout, andstderrare 0, 1, and 2, respectively. The preprocessor symbols Stdin_fileno, Stdout_fileno, and Stderr_fileno are with this values in < defined . (ApplyingFreopen(3) to one of those streams can change the file descriptor number associated with the stream.) ...
Stdin/stdout/stderr are pointer variables that point to the file type of the stream. When the program starts, the integer file descriptor (FD) associated with it is 0,1,2.
Stdin_fileno/stdout_fileno/stderr_fileno is a precompiled macro defined in the < unistd.h> file. The value is 0,1,2.
(By Freopen (3), you can change the stream value that is combined with these 3 file descriptors (FD))
In other words, when the program starts:the file descriptor (FD) corresponding to the files * Stdin/stdout/stderr is Stdin_fileno (0)/Stdout_fileno (1)/Stderr_fileno (2) respectively. However, you can use the file *freopen (const char *path, const char *mode, file *stream); To change so that the file descriptor (FD) corresponds to the other stream.
Stdin_fileno/stdout_fileno/stderr_fileno these three file descriptors (FD) can be changed by Freopen () to change the stream that is associated with it. (combined with that file stream, that file stream is the standard input output)
Standard input stdin standard output Stdouterr output stderr are changed separately.
Case Program
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define CHANGE_BY_FREOPEN/* need to be commented out, to switch * *
int main (int argc,char**argv) {
Char buf[]= "hello,world\n";
#ifdef Change_by_freopen
Freopen ("Stdout_text.txt", "w", stdout);
Freopen ("Stderr_text.txt", "W", stderr);/* stderr *
#endif
printf ("%s", buf);
Fwrite (Buf,strlen (BUF), 1,stdout);
Write (Stdout_fileno,&buf,strlen (BUF));
Perror ("error Out");/* stderr *
return (0);
}/* stdouttest.c * *
Compile the command to generate the executable file stdouttest$ gcc-o stdouttest stdouttest.c
① When #define Change_by_freopen is commented out, invalid: The output result in the Terminal shell window is:
$./stdouttest
Hello,worldhello,worldhello,worlderror Out:no Error
$
Note: 3 (Hello,world + line breaks). 1 Error output standard input output, or shell window.
② When #define Change_by_freopen, effective: The output in the Terminal shell window is:
$./stdouttest
Error Out:no Error
$
Note: In the shell window, there is only stderr output.
However, a file named Stdout_text.txt is created, and the contents of the file are: Hello,worldhello,worldhello,world
Note: 3 (Hello,world + line breaks). Standard output only, from the shell window, changed to Stdout_text.txt file.
Freopen ("Stderr_text.txt", "W", stderr);/* stderr * * If valid: ①shell, nothing is output. ②stdout_text.txt, Output 3 (Hello,world + newline characters). ③stderr_text.txt, Output 1 (Error Out:no error).