Use errno in Linux

Source: Internet
Author: User

Copyright statement: reprinted in the form of hyperlink to indicate the original source and author information and this statement http://www.blogbus.com/wzgyantai-logs/24470871.html

When the C api function in linux encounters an exception, the errno variable (which must include errno. h) assign an integer. Different values indicate different meanings. You can view the cause of the error by checking the value, in actual programming, this solution solves many seemingly inexplicable problems. However, errno is a number, and the specific meaning must be read in errno. h. It is very tedious to check every time. The following methods can be used to conveniently obtain error information:
(1) void perror (const char * s)
Function Description
Perror () is used to output the cause of the previous function error to the standard error (stderr). The string indicated by parameter s is printed first, followed by the error cause string. The cause of this error is determined based on the value of the global variable errno.
(2) char * strerror (int errno)
Convert the error code to a string error message. You can combine the string and other information and output it to the user interface, for example
Fprintf (stderr, "error in CreateProcess % s, Process ID % d", strerror (errno), processID)
Note: Assume processID is an acquired integer ID.
(3) printf ("% m", errno );

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.