When we write C programs, we often come across printf (), fprintf (), perror (), what exactly do these things do?I have to say stdin, stdout, stderr. Think about what we do when we write a File in C: File * FP =fopen(), which is what we request from the system, which is the equivalent of a channel to the File.
Actually, stdin,stdout,stderr is this FP, but it is turned on by default when the computer system is turned on, where 0 is stdin, which means input stream, which means input from the keyboard, 1 is STdout,2 is Stderr, and 1,2 is the monitor by default. While printf () prints out to stdout, which is equivalent to Fprintf (stdout, “***”), perror() prints out to Stderr, which is equivalent to Fprintf (stderr, “***”), what’s the difference between stdout and Stderr?
The reason we use printf () when we write a program is that we can monitor the health of our program, or debug, if our program is running all the time without stopping, we can’t keep staring at the screen to see the output of the program, then we can use file redirection. Will output to a file that we can look at later. For example, test.c
(CPP) view plain copy
1. & lt; The pre class = “CPP” name = “code” & gt; #include< stdio.h>
2.
3. Int main ()
4. {
5. The printf (” stdout Helo World!!!!! \n”);
6.
7. Return 0;
8.}
After compiling, we./test > Test.txt (redirects the contents of stdout to a file by default) outputs the contents of the test program to a file called test.txt. There is a more explicit way of writing it. Test.txt, where the 1 stands for stdout. Speaking of which, you should know what Stderr should do. Test.c: That’s right.
(CPP) view plain copy
1. # include< stdio.h>
2.
3. Int main ()
4. {
5. The printf (” Stdout Helo World!!!!! \n”);
6. Fprintf (stdout, stdout “Hello World!! \n”);
7. Perror (Stderr “Hello World!!!!! \n”);
8. Fprintf (stderr, “stderr Hello World!! \n”);
9.
10. Return 0;
11.}
After compilation,./test, four outputs on the screen, if./test > Test.ext, the result is two Stderr Hello World output from the screen!! , Stdout Helo World!!!!! In the file test.txt, based on the above, it is easy to understand the current result, so we can do whatever we want with the output, such as:
./test 1> testout.txt 2> Testerr.txt, we’ll output stdout to testout.txt, stderr to testerr.txt;
./test 1> Testout.txt, output stdout to file testout.txt, output stderr to screen;
./test 2> Testerr.txt, output stderr to file testerr.txt, output stdout to the screen;
./test > test.txt 2> & 1, this is to redirect stdout and Stderr to the same file, test.txt.
If we don’t want to see the output, either on the screen or redirected to a file, don’t worry, Linux has a solution for everything,./test >/dev/zero 2 & gt; & 1, so you don’t see any output.
Note: An important difference between Stderr and Stdout is that Stderr is non-buffered and outputs immediately, whereas Stdout defaults to row buffering, which means it outputs when it hits’ \n ‘. If you want stdout to output in real time as well, add Fflush (stdout) to the end of the output statement to do so in real time.
Reprinted in WeChat embedded ARM public number
Actually, stdin,stdout,stderr is this FP, but it is turned on by default when the computer system is turned on, where 0 is stdin, which means input stream, which means input from the keyboard, 1 is STdout,2 is Stderr, and 1,2 is the monitor by default. While printf () prints out to stdout, which is equivalent to Fprintf (stdout, “***”), perror() prints out to Stderr, which is equivalent to Fprintf (stderr, “***”), what’s the difference between stdout and Stderr?
The reason we use printf () when we write a program is that we can monitor the health of our program, or debug, if our program is running all the time without stopping, we can’t keep staring at the screen to see the output of the program, then we can use file redirection. Will output to a file that we can look at later. For example, test.c
(CPP) view plain copy
1. & lt; The pre class = “CPP” name = “code” & gt; #include< stdio.h>
2.
3. Int main ()
4. {
5. The printf (” stdout Helo World!!!!! \n”);
6.
7. Return 0;
8.}
After compiling, we./test > Test.txt (redirects the contents of stdout to a file by default) outputs the contents of the test program to a file called test.txt. There is a more explicit way of writing it. Test.txt, where the 1 stands for stdout. Speaking of which, you should know what Stderr should do. Test.c: That’s right.
(CPP) view plain copy
1. # include< stdio.h>
2.
3. Int main ()
4. {
5. The printf (” Stdout Helo World!!!!! \n”);
6. Fprintf (stdout, stdout “Hello World!! \n”);
7. Perror (Stderr “Hello World!!!!! \n”);
8. Fprintf (stderr, “stderr Hello World!! \n”);
9.
10. Return 0;
11.}
After compilation,./test, four outputs on the screen, if./test > Test.ext, the result is two Stderr Hello World output from the screen!! , Stdout Helo World!!!!! In the file test.txt, based on the above, it is easy to understand the current result, so we can do whatever we want with the output, such as:
./test 1> testout.txt 2> Testerr.txt, we’ll output stdout to testout.txt, stderr to testerr.txt;
./test 1> Testout.txt, output stdout to file testout.txt, output stderr to screen;
./test 2> Testerr.txt, output stderr to file testerr.txt, output stdout to the screen;
./test > test.txt 2> & 1, this is to redirect stdout and Stderr to the same file, test.txt.
If we don’t want to see the output, either on the screen or redirected to a file, don’t worry, Linux has a solution for everything,./test >/dev/zero 2 & gt; & 1, so you don’t see any output.
Note: An important difference between Stderr and Stdout is that Stderr is non-buffered and outputs immediately, whereas Stdout defaults to row buffering, which means it outputs when it hits’ \n ‘. If you want stdout to output in real time as well, add Fflush (stdout) to the end of the output statement to do so in real time.
Reprinted in WeChat embedded ARM public number
Read More:
- error C2057: expected constant expression (Can the size of an array in C language be defined when the program is running?)
- The function and usage of argc and argv in C language
- C language string processing error warning, c4996, sprintf, predicted, c4996, strcpy, c4996, strcat
- Error c2137 of C language: empty character constant (Fixed)
- C language — to solve the problem of program flashback when programming (in VS)
- C language error: stray ‘\ 240’ in program|
- Solution to error [error] LD returned 1 exit status in C language
- Solution to the segmentation fault of single chain table in C language
- 【.Net Core】using declarations‘ is not available in C# 7.3. Please use language version 8.0 or greate
- C language write() function analysis: write failed bad address
- The language of C__ FILE__ 、__ LINE__ And line
- On the coercion of C language
- R language – error analysis – error in Call.graphics (C_ palette2, .Call(C_ palette2, NULL)) : invalid graphics state
- MySQL advanced — Explanation of ref field in explain information
- The C language qsort() function reports an error for overflow of – 2147483648 and 2147483648
- [Linux] solve the error of decompressing JDK on Linux gzip: stdin: not in gzip format
- Error in installing pywin32 under Windows: close failed in file object D estructor:sys.excepthook is missing lost sys.stderr
- C language program running results flash back how to do
- Python3 reports an error [errno 5] input / output error. Print is used when there is no stdout