UNIX再学习 -- 错误和警告
錯誤和警告是常會出現的現象,了解它對以后解決問題會很有幫助。下面我們就重點來詳細介紹它們。
一、錯誤
1、回憶錯誤
我們之前講解其他內容時有涉及到錯誤的部分,下面讓我們來回憶一下:
(1)參看:C語言再學習 -- C 預處理器
#error ?字符串 => 表示產生一個錯誤信息
#warning 字符串 => 表示產生一個警告信息
(2)參看:C語言再學習 -- 關鍵字return和exit ()函數
C語言中通過使用返回來表示是否出錯,根據返回值來進行具體的錯誤處理一般規則:1)如果返回值類型時int類型,并且返回的值不可能是負數時,則使用返回值-1代表出錯,其他數據表示正常返回。
2)如果返回值類型時int類型,并且返回的值可能是負數時,則需要使用指針取出返回值的數據,返回值僅僅表示是否出錯,-1表示出錯,0表示正常返回。
3)如果返回值類型是指針類型,則返回值NULL代表出錯。
4)如果不考慮是否出錯,返回值類型使用void即可。
(3)參看:C語言再學習 -- 文件
stderr -- 標準錯誤輸出設備,例如:
fprintf(stderr, "Can't open it!\n");
(4)C語言再學習 -- EOF、feof函數、ferror函數
ferror () 函數用法。
2、errno 錯誤代碼
經常在調用 linux 系統 api 的時候會出現一些錯誤,比方說使用open () write () creat () 之類的函數有些時候會返回 -1,也就是調用失敗,這個時候往往需要知道失敗的原因。UNIX/Linux 為我們提供了外部全局變量 errno,當函數調用失敗,會將具體的錯誤編號設置到 errno ,我們可以通過 errno 來獲取錯誤的原因。下面我們來介紹 errno。(1)介紹errno
error 全局變量在 error.h 頭文件定義,extern int errno
在文件 /usr/include/errno.h /* Declare the `errno' variable, unless it's defined as a macro bybits/errno.h. This is the case in GNU, where it is a per-threadvariable. This redeclaration using the macro still works, but itwill be a function declaration without a prototype and may triggera -Wstrict-prototypes warning. */ #ifndef errno extern int errno; #endif 錯誤 Exx 的宏定義在 /usr/include/asm-generic 文件夾下面的 ?errno-base.h 和 errno.h,分別定義了 1-34 、35-132 的錯誤定義。
查看 /usr/include/asm-generic/errno-base.h #ifndef _ASM_GENERIC_ERRNO_BASE_H #define _ASM_GENERIC_ERRNO_BASE_H#define EPERM 1 /* Operation not permitted */ #define ENOENT 2 /* No such file or directory */ #define ESRCH 3 /* No such process */ #define EINTR 4 /* Interrupted system call */ #define EIO 5 /* I/O error */ #define ENXIO 6 /* No such device or address */ #define E2BIG 7 /* Argument list too long */ #define ENOEXEC 8 /* Exec format error */ #define EBADF 9 /* Bad file number */ #define ECHILD 10 /* No child processes */ #define EAGAIN 11 /* Try again */ #define ENOMEM 12 /* Out of memory */ #define EACCES 13 /* Permission denied */ #define EFAULT 14 /* Bad address */ #define ENOTBLK 15 /* Block device required */ #define EBUSY 16 /* Device or resource busy */ #define EEXIST 17 /* File exists */ #define EXDEV 18 /* Cross-device link */ #define ENODEV 19 /* No such device */ #define ENOTDIR 20 /* Not a directory */ #define EISDIR 21 /* Is a directory */ #define EINVAL 22 /* Invalid argument */ #define ENFILE 23 /* File table overflow */ #define EMFILE 24 /* Too many open files */ #define ENOTTY 25 /* Not a typewriter */ #define ETXTBSY 26 /* Text file busy */ #define EFBIG 27 /* File too large */ #define ENOSPC 28 /* No space left on device */ #define ESPIPE 29 /* Illegal seek */ #define EROFS 30 /* Read-only file system */ #define EMLINK 31 /* Too many links */ #define EPIPE 32 /* Broken pipe */ #define EDOM 33 /* Math argument out of domain of func */ #define ERANGE 34 /* Math result not representable */#endif 查看 /usr/include/asm-generic/errno.h #ifndef _ASM_GENERIC_ERRNO_H #define _ASM_GENERIC_ERRNO_H#include <asm-generic/errno-base.h>#define EDEADLK 35 /* Resource deadlock would occur */ #define ENAMETOOLONG 36 /* File name too long */ #define ENOLCK 37 /* No record locks available */ #define ENOSYS 38 /* Function not implemented */ #define ENOTEMPTY 39 /* Directory not empty */ #define ELOOP 40 /* Too many symbolic links encountered */ #define EWOULDBLOCK EAGAIN /* Operation would block */ #define ENOMSG 42 /* No message of desired type */ #define EIDRM 43 /* Identifier removed */ #define ECHRNG 44 /* Channel number out of range */ #define EL2NSYNC 45 /* Level 2 not synchronized */ #define EL3HLT 46 /* Level 3 halted */ #define EL3RST 47 /* Level 3 reset */ #define ELNRNG 48 /* Link number out of range */ #define EUNATCH 49 /* Protocol driver not attached */ #define ENOCSI 50 /* No CSI structure available */ #define EL2HLT 51 /* Level 2 halted */ #define EBADE 52 /* Invalid exchange */ #define EBADR 53 /* Invalid request descriptor */ #define EXFULL 54 /* Exchange full */ #define ENOANO 55 /* No anode */ #define EBADRQC 56 /* Invalid request code */ #define EBADSLT 57 /* Invalid slot */#define EDEADLOCK EDEADLK#define EBFONT 59 /* Bad font file format */ #define ENOSTR 60 /* Device not a stream */ #define ENODATA 61 /* No data available */ #define ETIME 62 /* Timer expired */ #define ENOSR 63 /* Out of streams resources */ #define ENONET 64 /* Machine is not on the network */ #define ENOPKG 65 /* Package not installed */ #define EREMOTE 66 /* Object is remote */ #define ENOLINK 67 /* Link has been severed */ #define EADV 68 /* Advertise error */ #define ESRMNT 69 /* Srmount error */ #define ECOMM 70 /* Communication error on send */ #define EPROTO 71 /* Protocol error */ #define EMULTIHOP 72 /* Multihop attempted */ #define EDOTDOT 73 /* RFS specific error */ #define EBADMSG 74 /* Not a data message */ #define EOVERFLOW 75 /* Value too large for defined data type */ #define ENOTUNIQ 76 /* Name not unique on network */ #define EBADFD 77 /* File descriptor in bad state */ #define EREMCHG 78 /* Remote address changed */ #define ELIBACC 79 /* Can not access a needed shared library */ #define ELIBBAD 80 /* Accessing a corrupted shared library */ #define ELIBSCN 81 /* .lib section in a.out corrupted */ #define ELIBMAX 82 /* Attempting to link in too many shared libraries */ #define ELIBEXEC 83 /* Cannot exec a shared library directly */ #define EILSEQ 84 /* Illegal byte sequence */ #define ERESTART 85 /* Interrupted system call should be restarted */ #define ESTRPIPE 86 /* Streams pipe error */ #define EUSERS 87 /* Too many users */ #define ENOTSOCK 88 /* Socket operation on non-socket */ #define EDESTADDRREQ 89 /* Destination address required */ #define EMSGSIZE 90 /* Message too long */ #define EPROTOTYPE 91 /* Protocol wrong type for socket */ #define ENOPROTOOPT 92 /* Protocol not available */ #define EPROTONOSUPPORT 93 /* Protocol not supported */ #define ESOCKTNOSUPPORT 94 /* Socket type not supported */ #define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */ #define EPFNOSUPPORT 96 /* Protocol family not supported */ #define EAFNOSUPPORT 97 /* Address family not supported by protocol */ #define EADDRINUSE 98 /* Address already in use */ #define EADDRNOTAVAIL 99 /* Cannot assign requested address */ #define ENETDOWN 100 /* Network is down */ #define ENETUNREACH 101 /* Network is unreachable */ #define ENETRESET 102 /* Network dropped connection because of reset */ #define ECONNABORTED 103 /* Software caused connection abort */ #define ECONNRESET 104 /* Connection reset by peer */ #define ENOBUFS 105 /* No buffer space available */ #define EISCONN 106 /* Transport endpoint is already connected */ #define ENOTCONN 107 /* Transport endpoint is not connected */ #define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */ #define ETOOMANYREFS 109 /* Too many references: cannot splice */ #define ETIMEDOUT 110 /* Connection timed out */ #define ECONNREFUSED 111 /* Connection refused */ #define EHOSTDOWN 112 /* Host is down */ #define EHOSTUNREACH 113 /* No route to host */ #define EALREADY 114 /* Operation already in progress */ #define EINPROGRESS 115 /* Operation now in progress */ #define ESTALE 116 /* Stale NFS file handle */ #define EUCLEAN 117 /* Structure needs cleaning */ #define ENOTNAM 118 /* Not a XENIX named type file */ #define ENAVAIL 119 /* No XENIX semaphores available */ #define EISNAM 120 /* Is a named type file */ #define EREMOTEIO 121 /* Remote I/O error */ #define EDQUOT 122 /* Quota exceeded */#define ENOMEDIUM 123 /* No medium found */ #define EMEDIUMTYPE 124 /* Wrong medium type */ #define ECANCELED 125 /* Operation Canceled */ #define ENOKEY 126 /* Required key not available */ #define EKEYEXPIRED 127 /* Key has expired */ #define EKEYREVOKED 128 /* Key has been revoked */ #define EKEYREJECTED 129 /* Key was rejected by service *//* for robust mutexes */ #define EOWNERDEAD 130 /* Owner died */ #define ENOTRECOVERABLE 131 /* State not recoverable */#define ERFKILL 132 /* Operation not possible due to RF-kill */#define EHWPOISON 133 /* Memory page has hardware error */#endif 還有一些更大的錯誤號是留給內核級別的,如系統調用等,用戶程序一般是看不見的這些號的,Ubuntu12.04中/usr/src/linux-headers-3.2.0-23-generic-pae/include/linux/errno.h?
查看 /usr/src/linux-headers-3.2.0-23-generic-pae/include/linux/errno.h #ifndef _LINUX_ERRNO_H #define _LINUX_ERRNO_H#include <asm/errno.h>#ifdef __KERNEL__/** These should never be seen by user programs. To return one of ERESTART** codes, signal_pending() MUST be set. Note that ptrace can observe these* at syscall exit tracing, but they will never be left for the debugged user* process to see.*/ #define ERESTARTSYS 512 #define ERESTARTNOINTR 513 #define ERESTARTNOHAND 514 /* restart if no handler.. */ #define ENOIOCTLCMD 515 /* No ioctl command */ #define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall *//* Defined for the NFSv3 protocol */ #define EBADHANDLE 521 /* Illegal NFS file handle */ #define ENOTSYNC 522 /* Update synchronization mismatch */ #define EBADCOOKIE 523 /* Cookie is stale */ #define ENOTSUPP 524 /* Operation is not supported */ #define ETOOSMALL 525 /* Buffer or request is too small */ #define ESERVERFAULT 526 /* An untranslatable error occurred */ #define EBADTYPE 527 /* Type not supported by server */ #define EJUKEBOX 528 /* Request initiated, but will not complete before timeout */ #define EIOCBQUEUED 529 /* iocb queued, will get completion event */ #define EIOCBRETRY 530 /* iocb queued, will trigger a retry */#endif#endif
(2)errno 轉換成 字符串
1)strerror 函數
#include <string.h>char *strerror(int errnum);
函數功能:
主要用于將參數指定的錯誤編號翻譯成對應的錯誤信息返回。
#include <stdio.h> #include <errno.h> #include <string.h>extern int errno ;int main () {FILE *fp;// file.txt 不存在fp = fopen("file.txt", "r");if( fp == NULL ) {fprintf(stderr, "錯誤碼: %d\n", errno);fprintf(stderr, "對應錯誤信息為: %s\n", strerror(errno));}else {fclose(fp);}return(0); } 輸出結果: 錯誤碼: 2 對應錯誤信息為: No such file or directory
2)perror 函數 (重點)
#include <stdio.h>void perror(const char *s);
函數功能: 表示將最后一個錯誤信息打印出來,參數 s 不為空時原樣輸出,后面追加一個冒號和空格,再跟著錯誤信息,以及換行。#include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h>extern int errno ;int main (void) {FILE *fp;// file.txt 不存在fp = fopen("file.txt", "r"); if( fp == NULL ) { printf("錯誤碼 = %d\n",errno); perror ("打開失敗"), exit (-1); } else { fclose(fp); } return(0); } 輸出結果: 錯誤碼 = 2 打開失敗: No such file or directory
3)printf 函數
使用 printf("%m\n"); ?%m 格式化標記打印錯誤信息?#include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h>extern int errno ;int main (void) {FILE *fp;// file.txt 不存在fp = fopen("file.txt", "r"); if( fp == NULL ) { printf("錯誤碼 = %d\n",errno); printf ("%m\n"); } else { fclose(fp); } return(0); } 輸出結果: 錯誤碼 = 2 No such file or directory(3)不能根據錯誤號判斷是否出錯
雖然所有的錯誤號都不是零,但是因為函數執行成功的情況下錯誤號全局變量 errno 不會被修改,所以不能用該變量的值為零或非零,作為出錯或沒出錯的判斷依據。例如:#include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h>extern int errno ;int main () {FILE *fp;// file.txt 不存在 fp = fopen("file.txt", "r");FILE *fp_test;// test.txt 存在 fp_test = fopen("test.txt", "r");if(errno) //不能根據 errno 判斷是否出錯{ fprintf (stderr, "打開失敗\n");}else {fclose(fp_test);}return(0); } 輸出結果: 打開失敗 上述例子,就可以看出本來不應該打印 "打開失敗" 的。 如果非要使用錯誤號判斷是否出錯也可以,那你在調用它之前必須手動將errno清零。 #include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h>extern int errno ;int main () {FILE *fp;// file.txt 不存在 fp = fopen("file.txt", "r");errno = 0; //將 errno 清零FILE *fp_test;// test.txt 存在 fp_test = fopen("test.txt", "r");if(errno) //不能根據 errno 判斷是否出錯{ fprintf (stderr, "打開失敗\n");}else {fclose(fp_test);}return(0); } 正確的做法是,先根據函數的返回值判斷是否出錯,在確定出錯的前提下再根據 errno 的值判斷具體出了什么錯。 #include <stdio.h> #include <errno.h> #include <string.h>extern int errno ;int main () {FILE *fp;// file.txt 不存在fp = fopen("file.txt", "r");if( fp == NULL ) {fprintf(stderr, "錯誤碼: %d\n", errno);fprintf(stderr, "對應錯誤信息為: %s\n", strerror(errno));}else {fclose(fp);}return(0); } 輸出結果: 錯誤碼: 2 對應錯誤信息為: No such file or directory二、編譯錯誤和警告
1、上面提到的 errno 是標準庫函數的錯誤代碼,現在來看看gcc編譯錯誤和警告
參看:C語言再學習 -- GCC編譯過程(1)讓所有編譯警告都顯示出來,選項 -Wall
如下,編輯一段警告的代碼 #include <stdio.h> int main (void) { int i; printf ("\n hello world![i]\n", i); return 0; } root@ubuntu:/home/tarena/project/c_test# gcc -Wall hello.c -o hello hello.c: 在函數‘main’中: hello.c:6:2: 警告: 提供給格式字符串的實參太多 [-Wformat-extra-args] hello.c:6:9: 警告: 此函數中的‘i’在使用前未初始化 [-Wuninitialized](2)將編譯警告轉換成錯誤的選項 -Werror
編譯警告很多時候會被我們忽視,在特殊場合我們還是需要重視編譯警告的,如果能把編譯警告變成直接輸出錯誤,那我們的重視程度會提高很多并去解決。#include <stdio.h> int main (void) { int i; printf ("\n hello world![i]\n", i); return 0; } root@ubuntu:/home/tarena/project/c_test# gcc -Wall -Werror hello.c hello.c: 在函數‘main’中: hello.c:6:2: 錯誤: 提供給格式字符串的實參太多 [-Werror=format-extra-args] cc1: all warnings being treated as errors
(3)警告級別
如果你覺得警告級別不夠,可以使用更高的警告級別。參看:Options to Request or Suppress Warnings
2、C語言編譯錯誤及警告對照表?
參看:c語言的錯誤及警告對照表參看:16種C語言編譯警告(Warning)類型的解決方法
3、將警告,錯誤等信息輸出到文件中
參看:將Linux腳本中的正常輸出,警告,錯誤等信息輸出到文件中(1)其中標準輸出、標準輸出、標準錯誤,可參看:C語言再學習 -- 文件
C 程序自動打開3個文件。這3個文件被稱為標準輸入,標準輸出和標準錯誤輸出。默認的標準輸入是系統的一般輸入設備,通常為鍵盤;默認的標準輸出和標準錯誤輸出是系統的一般輸出設備,通常為顯示器,分別得到文件描述符 0, 1, 2.
下面的方法從標準輸入(鍵盤)獲得一個字符: ?ch = getchar ( );
標準文件指針:
stdio.h文件把3個文件指針與3個C 程序自動打開的標準文件進行了并聯,如下表所示:
標準文件 ? | 文件指針 ? | 一般使用的設備 ? |
標準輸入 | stdin | 鍵盤 |
標準輸出 | stdout | 顯示器 |
標準錯誤 | stderr | 顯示器 |
這些指針都是FILE指針類型,所以可以被用作標準I/O函數的參數。
(2)下面以make命令為例來說明,如何把對應的信息,輸出到對應的文件中:
【用法】1)想要把make輸出的全部信息,輸出到某個文件中,最常見的辦法就是:
make xxx > build_output.txt
此時默認情況是沒有改變2=stderr的輸出方式,還是屏幕,所以,如果有錯誤信息,還是可以在屏幕上看到的。
2)只需要把make輸出中的錯誤(及警告)信息輸出到文件中ing,可以用:
make xxx 2> build_output.txt
相應地,由于1=stdout沒有變,還是屏幕,所以,那些命令執行時候輸出的正常信息,還是會輸出到屏幕上,你還是可以在屏幕上看到的。
3)只需要把make輸出中的正常(非錯誤,非警告)的信息輸出到文件中,可以用:
make xxx 1> build_output.txt
相應地,由于2=stderr沒有變,還是屏幕,所以,那些命令執行時候輸出的錯誤信息,還是會輸出到屏幕上,你還是可以在屏幕上看到的。
4)想要把正常輸出信息和錯誤信息輸出到分別的文件中,可以用:
make xxx 1> build_output_normal.txt 2>build_output_error.txt
即聯合使用了1和2,正常信息和錯誤信息,都輸出到對應文件中了。
5)所有的信息都輸出到同一個文件中:
make xxx > build_output_all.txt 2>&1
其中的2>&1表示錯誤信息輸出到&1中,而&1,指的是前面的那個文件:build_output_all.txt 。
注意:上面所有的1,2等數字,后面緊跟著大于號'>' ,中間不能有空格。
總結
以上是生活随笔為你收集整理的UNIX再学习 -- 错误和警告的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 互联网晚报 | 04月05日 星期二 |
- 下一篇: servlet 调用oracle数据库存