当前位置:网站首页>Errno perrno and strerrno

Errno perrno and strerrno

2022-06-25 14:59:00 qq_ twenty-three million nine hundred and fifty-three thousand

errno

 The header file <errno.h>
errno It's a by POSIX and ISO C Symbols defined by the standard . When an error occurs in a system call or library function , For example, when opening a nonexistent file in an institutional way , Its value will be changed , according to errno The difference in value , We can see what went wrong with the program .
 Such as error code errno by 12  The error is Cannot allocate memory

perror

The function prototype :void perror(const char *s)
Function description : Output the cause of the error in the previous function to the standard error (stderr), Parameters s The string pointed to will be printed out first , Then print the real error output , Such as perror(“ABC:”), String is ABC:Cannot allocate memory

strerror

The function prototype :void strerror(int errno)
Function description : Convert the error code to a string error message , This string and other information can be combined and output to the user interface , Such as printf(“errno :%d, %s”, errno, strerror(errno)) The output value is errno:12,Cannot allocate memory

matters needing attention

1, If the system call or library function is executed correctly ,errno The value of is not cleared , If you execute a function A There was a mistake errno Be changed , Next, execute the function directly B, If the function B If executed correctly ,errno Also keep functions A The value set when an error occurs .
2, The system call or library function is executed correctly , There is no guarantee that errno The value of will not be changed
3, All error numbers are non - 0 Of

Sum up : When needed errno To determine whether the function is executed correctly , take errno Set up 0, At the end of function execution , Determine whether the function is executed correctly , When an execution error occurs , Follow up errno To determine exactly what went wrong .

原网站

版权声明
本文为[qq_ twenty-three million nine hundred and fifty-three thousand ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200514582002.html