詳解C/C++ Linux出錯(cuò)處理函數(shù)(strerror與perror)的使用
前言
我們知道,系統(tǒng)函數(shù)調(diào)用不能保證每次都成功,必須進(jìn)行出錯(cuò)處理,這樣一方面可以保證程序邏輯正常,另一方面可以迅速得到故障信息。
有兩種方式:
1. strerror - 需要將錯(cuò)誤信息輸出到日志;
2. perror - 不需要將錯(cuò)誤信息輸出到日志。
一、strerror
#include <errno.h>
#include <string.h>
char * strerror (int errnum); /* See NOTES */
errnum:
傳入?yún)?shù),錯(cuò)誤編號(hào)的值,一般取 errno 的值;
返回值:
錯(cuò)誤原因;
作用:
調(diào)用任何系統(tǒng)函數(shù),當(dāng)出錯(cuò)后,系統(tǒng)都會(huì)將錯(cuò)誤代號(hào)賦值給全局變量errno,當(dāng)出現(xiàn)錯(cuò)誤后,我們就可以使用函數(shù)strerror,傳參errno,就會(huì)返回對(duì)應(yīng)的出錯(cuò)信息了。
具體使用案例:
fprintf(stderr, "error, reason: %s\n", strerror(errno));
為什么說,需要將錯(cuò)誤信息輸出到日志文件時(shí)要使用strerror函數(shù)呢,因?yàn)閟trerror函數(shù)轉(zhuǎn)換后返回char *指針,我們拿到這個(gè)指針后就可以將這個(gè)指針字符串寫入日志文件中去啦。
下面會(huì)講解為什么使用perno函數(shù)就不可以這樣操作!
例:
1. 打開文件失敗
#include <errno.h> // errno #include <string.h> // strerror #include <stdio.h> // printf #include <stdlib.h> // exit int main (int argc, char *argv[]) { FILE *fp = NULL; fp = fopen("./123/test.txt", "r"); if (NULL == fp) { char *err = strerror(errno); fprintf(stderr, "open file error, reason: %s\n", err); //fprintf(stderr, "open file error, reason: %s\n", strerror(errno)); exit(-1); } printf("open file success!\n"); return 0; }
編譯運(yùn)行:
root@YGT:/home/ygt/echo_server/test# gcc errno.c -o errno
root@YGT:/home/ygt/echo_server/test# ./errno
open file error, reason: No such file or directory
其中,“No such file or directory” 就是我們通過strerror函數(shù)獲取到的錯(cuò)誤信息!
2. 創(chuàng)建socket失敗
#include <stdio.h> // printf #include <sys/types.h> // socket #include <sys/socket.h> // socket #include <stdlib.h> // exit #include <errno.h> // errno #include <string.h> // strerror int main (int argc, char *argv[]) { int sock = 0; sock = socket(-1, SOCK_STREAM, 0); if (-1 == sock) { fprintf(stderr, "create socket error, reason: %s\n", strerror(errno)); exit(-1); } return 0; }
編譯運(yùn)行:
root@YGT:/home/ygt/echo_server/test# gcc errno.c -o errno
root@YGT:/home/ygt/echo_server/test# ./errno
create socket error, reason: Address family not supported by protocol
其中,“Address family not supported by protocol” 就是我們通過strerror函數(shù)獲取到的錯(cuò)誤信息!
二、perror
#include <stdio.h>
#include <errno.h>
void perror (const char *s); /* See NOTES */
s:
傳入?yún)?shù),自定義的描述;
返回值:
無;
作用:
向標(biāo)準(zhǔn)出錯(cuò)stderr 輸出錯(cuò)原因 。
具體使用案例:
perror("create socket error ");
將需要提示的字符串傳參給perror函數(shù)即可,它相當(dāng)于:
fprintf(stderr, "create socket error : ", strerror(errno));
perror是直接向出錯(cuò)標(biāo)準(zhǔn)stderr輸出錯(cuò)誤原因!
例:
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <stdlib.h> #include <errno.h> #include <string.h> int main (int argc, char *argv[]) { int sock = 0; sock = socket(-1, SOCK_STREAM, 0); if (-1 == sock) { perror("create socket error "); exit(-1); } return 0; }
編譯運(yùn)行:
root@YGT:/home/ygt/echo_server/test# gcc errno.c -o errno
root@YGT:/home/ygt/echo_server/test# ./errno
create socket error : Address family not supported by protocol
其中,“Address family not supported by protocol” 就是我們通過perror函數(shù)輸出的錯(cuò)誤信息!
因?yàn)樗侵苯酉虺鲥e(cuò)標(biāo)準(zhǔn)stderr輸出錯(cuò)誤原因,我們沒法通過它獲得錯(cuò)誤信息字符串,所以也就沒法將其寫入日志文件中!
最后,調(diào)用系統(tǒng)的任何函數(shù),當(dāng)出現(xiàn)了錯(cuò)誤,都可以使用以上兩種方式進(jìn)行打印輸出錯(cuò)誤信息,從而更輕易的幫助我們找出對(duì)應(yīng)問題!
如果需要將錯(cuò)誤信息輸出到日志文件,使用strerror 函數(shù);否則可以使用perror函數(shù)。
注意:以上介紹的 適用于任何Linux函數(shù),例如open、write等。
到此這篇關(guān)于詳解C/C++ Linux出錯(cuò)處理函數(shù)(strerror與perror)的使用的文章就介紹到這了,更多相關(guān)C++ Linux出錯(cuò)處理函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
數(shù)據(jù)結(jié)構(gòu)之位圖(bitmap)詳解
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)之位圖詳解,本文講解了位圖的基本知識(shí)、位圖的實(shí)現(xiàn)方法、位圖的應(yīng)用等內(nèi)容,需要的朋友可以參考下2014-08-08C語言實(shí)現(xiàn)二值圖像模擬灰值圖像顯示效果
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)二值圖像模擬灰值圖像顯示效果,分為圖案法、抖動(dòng)法兩個(gè)方法實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10C++ 中strcpy標(biāo)準(zhǔn)寫法實(shí)例詳解
這篇文章主要介紹了C++ 中strcpy標(biāo)準(zhǔn)寫法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06