Stdin, stdout, stderr

Source: Internet
Author: User
When a C language program is started, the operating system environment is responsible for opening three files and providing pointers to these three files to the program. The three files are standard input (stdin), standard output (stdout), and standard error (stderr ). They are declared in stdio. h. In most environments, stdin indicates...

When a C language program is started, the operating system environment is responsible for opening three files and providing pointers to these three files to the program. The three files are standard input (stdin), standard output (stdout), and standard error (stderr ). They declare in <stdio. h> that in most environments, stdin points to the keyboard, stdout, and stderr points to the display. If stderr is used, for some reason, one of the files cannot be accessed, the corresponding diagnostic information must be printed at the end of the output of the link. This processing method is acceptable when it is output to the screen, but it is unacceptable if it is output to a file or to another program through a pipeline. If stderr exists, even if the standard output is redirected, the output written to stderr is usually displayed on the screen.

Exit (): In the main program, the return expr statement is equivalent to exit (expr ). However, function exit has an advantage that it can be called from other functions and can be searched for by lookup programs. Exit (0) indicates normal exit. exit (1) indicates abnormal exit as long as the parameter in it is not zero ,.

Fgets:

Char * fgets (char * line, int maxline, FILE * fp );

Fp points to the file to read the next input line (including line breaks) and store it in the character array line, which reads up to maxline-1 characters. The row to be read ends with '\ 0' and ends with an array. Generally, fgets returns line. If the end of a file or an error occurs, NULL is returned.

 

I made a question at night and it took a long time to record it here because of some simple errors. C programming language version 2nd (Chinese) P145 7-6:

Compile a program, compare two files, and print their first line that is different.

1 # include <stdio. h>
2
3 # define MAXSIZE 100
4
5 main (int argc, char * argv [])
6 {
7 FILE * fp1, * fp2;
8 void filecmp (FILE *, FILE *);
9 char * prog = argv [0];
10
11 if (argc <3 ){
12 fprintf (stderr, "error: no enough files! \ N ");
13 exit (1 );
14}
15 else {
16 if (fp1 = fopen (* ++ argv, "r") = NULL ){
17 fprintf (stderr, "% s: can't open % s \ n", prog, * argv );
18 exit (2 );
19}
20 else if (fp2 = fopen (* ++ argv, "r") = NULL ){
21 fprintf (stderr, "% s: can't open % s \ n", prog, * argv );
22 exit (3 );
23}
24 else {
25 filecmp (fp1, fp2 );
26 fclose (fp1 );
27 fclose (fp2 );
28}
29}
30 exit (0 );
31}
32
33 void filecmp (FILE * ifp1, FILE * ifp2)
34 {
35 char line1 [MAXSIZE], line2 [MAXSIZE];
36
37 int count = 0;
38 int full = 0;
39
40 while (fgets (line1, MAXSIZE, ifp1 )! = NULL )&&
41 (fgets (line2, MAXSIZE, ifp2 )! = NULL )){
42 if (strcmp (line1, line2) = 0 ){
43 count ++;
44 full = 1;
45}
46 else {
47 fprintf (stdout, "The number of different line is % d \ n", count );
48 fprintf (stdout, "% s \ n", line1 );
49 fprintf (stdout, "% s \ n", line2 );
50 return;
51}
52}
53 if (! Full)
54 fprintf (stderr, "error: cannont read! \ N ");
55 else
56 fprintf (stderr, "same files! \ N ");
57}

Error 1: Write 35th rows:

Char * line1, * line2;

The relationship between arrays and pointers is obviously obfuscated here. If it is used as an array, it is actually the first number of addresses, rather than the entire array.

Error 2: After row 42nd, two sentences are added:

Fp1 ++;
Fp2 ++;

Fp1 and fp2 actually point to the file descriptor and cannot be added or subtracted. According to my intention, fp1 is automatically added and points to the next row of the file. In fact, the fgets function has added a line break when reading the next row.

The fgets function is as follows:

1 char * fgets (char * s, int n, FILE * fp)
2 {
3 register int c;
4 register char * cs;
5
6 while (-- n> 0 & (c = getc (iop ))! = EOF)
7 if (* cs ++ = c) = '\ n ')
8 break;
9 * cs = '\ 0 ';
10 return (c = EOF & cs = s )? NUll: s;
11}

The first line is clearly written, and the line break is read.

Conclusion: you must first understand the usage of each function parameter before doing so. Otherwise, it will be more than half the effort.

 

 

 

 

Descriptions of stdin, stdout, and stderr are as follows:

By default, standard input is read from the keyboard, while standard output and standard error are printed to the screen

By default, the standard input is read from the keyboard, and both the standard output and standard error are printed to the screen.

Test in the console:

Void main (void)
{
Fprintf (stderr, "% s: % d", _ FILE __, _ LINE __);
System ("pause ");
}

The path of the current file and the number of rows of the fprintf statement are displayed on the screen. Pai_^

Stdin, stdout, stderr

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.