C語言標準庫中的錯誤報告用法有三種形式。
1、errno
errno在<errno.h>頭文件中定義,如下
#ifndef errno extern int errno; #endif
外部變量errno保存庫程序中實現定義的錯誤碼,通常被定義為errno.h中以E開頭的宏,
所有錯誤碼都是正整數,如下例子
# define EDOM 33 /* Math argument out of domain of function. */
EDOM的意思是參數不在數學函數能接受的域中,稍后的例子中用到了這個宏。
errno的常見用法是在調用庫函數之前先清零,隨后再進行檢查。
在linux中使用c語言編程時,errno是個很有用的動動。他可以把最后一次調用c的方法的錯誤代碼保留。但是如果最后一次成功的調用c的方法,errno不會改變。因此,只有在c語言函數返回值異常時,再檢測errno。
errno會返回一個數字,每個數字代表一個錯誤類型。詳細的可以查看頭文件。/usr/include/asm/errno.h
如何把errno的數字轉換成相應的文字說明?
一個簡單的例子
#include <stdio.h> #include <errno.h> #include <string.h> #include <math.h> int main(void) { errno = 0; int s = sqrt(-1); if (errno) { printf("errno = %d/n", errno); // errno = 33 perror("sqrt failed"); // sqrt failed: Numerical argument out of domain printf("error: %s/n", strerror(errno)); // error: Numerical argument out of domain } return 0;
2、strerror
strerror在<string.h>中定義,如下
__BEGIN_NAMESPACE_STD
/* Return a string describing the meaning of the `errno' code in ERRNUM. */
extern char *strerror (int __errnum) __THROW;
__END_NAMESPACE_STD
函數strerror返回一個錯誤消息字符串的指針,其內容是由實現定義的,字符串不能修改,但可以在后續調用strerror函數是覆蓋。
char *strerror(int errno)
使用方式如下:
fprintf(stderr,"error in CreateProcess %s, Process ID %d ",strerror(errno),processID)
將錯誤代碼轉換為字符串錯誤信息,可以將該字符串和其它的信息組合輸出到用戶界面。
注:假設processID是一個已經獲取了的整形ID
3、perror
perror在<stdio.h>中定義,如下
__BEGIN_NAMESPACE_STD
/* Print a message describing the meaning of the value of errno.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void perror (const char *__s);
__END_NAMESPACE_STD
函數perror在標準錯誤輸出流中打印下面的序列:參數字符串s、冒號、空格、包含errno中當前錯誤碼的錯誤短消息和換行符。在標準C語言中,如果s是NULL指針或NULL字符的指針,則只打印錯誤短消息,而不打印前面的參數字符串s、冒號及空格。
void perror(const char *s)
函數說明
perror ( )用來將上一個函數發生錯誤的原因輸出到標準錯誤(stderr),參數s 所指的字符串會先打印出,后面再加上錯誤原因 字符串。此錯誤原因依照全局變量 errno 的值來決定要輸出的字符串。
另外并不是所有的c函數調用發生的錯誤信息都會修改errno。例如gethostbyname函數。
errno是否是線程安全的?
errno是支持線程安全的,而且,一般而言,編譯器會自動保證errno的安全性。
我們看下相關頭文件 /usr/include/bits/errno.h
會看到如下內容:
# if !defined _LIBC || defined _LIBC_REENTRANT/* When using threads, errno is a per-thread value. */# define errno (*__errno_location ())# endif# endif /* !__ASSEMBLER__ */#endif /* _ERRNO_H */
也就是說,在沒有定義__LIBC或者定義_LIBC_REENTRANT的時候,errno是多線程/進程安全的。
為了檢測一下你編譯器是否定義上述變量,不妨使用下面一個簡單程序。
#include <stdio.h>#include <errno.h> int main( void ){#ifndef __ASSEMBLER__ printf( "Undefine __ASSEMBLER__/n" );#else printf( "define __ASSEMBLER__/n" );#endif #ifndef __LIBC printf( "Undefine __LIBC/n" );#else printf( "define __LIBC/n" );#endif #ifndef _LIBC_REENTRANT printf( "Undefine _LIBC_REENTRANT/n" );#else printf( "define _LIBC_REENTRANT/n" );#endif return 0;}
新聞熱點
疑難解答
圖片精選