The difference between Stdin,stdout,stderr and Stdin_fileno,stdout_fileno,stderr_fileno

Source: Internet
Author: User
Tags stdin strlen

First you know the difference between FILE *stdout and Stdout_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 those 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 stdout err output stderr changed separately.

Case Program

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>


#define Change_by_freopen

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);
#endif

printf ("%s", buf);
Fwrite (Buf,strlen (BUF), 1,stdout);
Write (Stdout_fileno,&buf,strlen (BUF));

Perror ("error out");


return (0);
}


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,world Hello,world hello,world error 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 that contains the contents of the file: Hello,world hello,world Hello,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); If it works: in ①shell, nothing is output. ②stdout_text.txt, Output 3 (Hello,world + newline characters). ③stderr_text.txt, Output 1 (Error Out:no error).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.