C Language Learning 008: standard error, C Language Learning 008 Standard

Source: Internet
Author: User

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.

Related Article

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.