Errno used in Linux (GO)

Source: Internet
Author: User

When the C API function in Linux exception, the errno variable (need include errno.h) is usually assigned an integer value, different values represent different meanings, you can see the reason for this value speculation, in the actual programming with this trick to solve a lot of the original seemingly inexplicable problems. But errno is a number, representing the specific meaning of the errno.h to read the macro definition, and each review is a very tedious thing.

There are several ways to easily get the error message
(1)void perror (const char *s)
Function description
Perror () is used to output the cause of an error in the previous function to a standard error (STDERR), and the string referred to in parameter S is printed first, followed by the error reason string. The reason for this error is to determine the string to be output according to the value of the global variable errno.
(2)char *strerror (int errno)
Convert the error code tostring Error message, the string and other information can be combined to output to the user interface such as
fprintf (stderr, "Error in CreateProcess%s, Process ID%d", strerror (errno), ProcessID)
Note: Suppose ProcessID is an ID that has been acquired
(3)printf ("%m", errno);
It's not all the time. Error codes can be obtained by using error, such as the following code snippet

#include "stdio.h"
#include "Stdlib.h"
#include "errno.h"
#include "Netdb.h"
#include "Sys/types.h"
#include "Netinet/in.h"
int main (int argc, char *argv[])
{
struct Hostent *h;
if (argc! = 2)
{
fprintf (stderr, "Usage:getip address\n");
Exit (1);
}

if ((H=gethostbyname (argv[1)) = = NULL)
{

Herror ("gethostbyname");
Exit (1);
}

printf ("Host Name:%s\n", h->h_name);
printf ("IP Address:%s\n", Inet_ntoa (* (struct in_addr *) h->h_addr));
return 0;
}


As you can see from the code above: using the gethostbyname () function, you cannot use perror () to output an error message because the error code is stored in H_errno instead of errno. So, you need to call the herror () function.
You simply pass to gethostbyname () a machine name ("bbs.tsinghua.edu.cn"), and then you get additional information such as IP from the returned struct struct hostent. The program that outputs the IP address needs to be explained: H->h_addr is a char*, but the Inet_ntoa () function needs to pass a struct IN_ADDR structure. So the h->h_addr is cast to struct in_addr*, and all the data is obtained through it.

Errno used in Linux (GO)

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.