C Language Learning 008: standard error, C Language Learning 008 Standard
In the data file in the previous section (C Language Learning 007: redirecting standard input and output), if the data in the file contains illegal data, how can the program display an error message? Standard errors are required.
1 # include <stdio. h> 2 3 int main () {4 float latitude; 5 float longpolling; 6 char info [80]; 7 int started = 0; 8 puts ("data ["); 9 while (scanf ("% f, % f, % 79 [^ \ n]", & latitude, & longpolling, info) = 3) {10 if (started) {11 printf (", \ n"); 12} 13 else {14 started = 1; 15} 16 if (latitude <-90.0) | (latitude> 90.0 )) {17 fprintf (stderr, "Invalid latitude: % f \ n", latitude); // fprintf can send files to standard output (stdout ), you can also send a standard error (stderr) 18 return 2; 19} 20 if (longpolling <-180.0) | (longpolling> 18.0) {21 fprintf (stderr, "Invalid longtitude: % f \ n", longpolling); 22 return 2; 23} 24 printf ("{latitude: % f, longpolling: % f, info: '% s'} ", latitude, longpolling, info); 25} 26 puts (" \ n] "); 27 return 0; 28}
We can see that the ">" operator is used to redirect the output to the output. json file, and the error message can still be seen on the screen. The purpose of creating a standard error is to distinguish between common output and error messages.
What's more, we can use the "2>" operator to redirect standard errors.