日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

詳解C/C++?Linux出錯處理函數(strerror與perror)的使用_C 語言

作者:cpp_learners ? 更新時間: 2023-02-14 編程語言

前言

我們知道,系統函數調用不能保證每次都成功,必須進行出錯處理,這樣一方面可以保證程序邏輯正常,另一方面可以迅速得到故障信息。

有兩種方式:

1. strerror - 需要將錯誤信息輸出到日志;

2. perror - 不需要將錯誤信息輸出到日志。

一、strerror?

#include <errno.h>
#include <string.h>
char * strerror (int errnum); ? ? /* See NOTES */

errnum:

傳入參數,錯誤編號的值,一般取 errno 的值;

返回值:

錯誤原因;

作用:

調用任何系統函數,當出錯后,系統都會將錯誤代號賦值給全局變量errno,當出現錯誤后,我們就可以使用函數strerror,傳參errno,就會返回對應的出錯信息了。

具體使用案例:

fprintf(stderr, "error, reason: %s\n", strerror(errno));

為什么說,需要將錯誤信息輸出到日志文件時要使用strerror函數呢,因為strerror函數轉換后返回char *指針,我們拿到這個指針后就可以將這個指針字符串寫入日志文件中去啦。

下面會講解為什么使用perno函數就不可以這樣操作!

例:

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;
}

編譯運行:

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函數獲取到的錯誤信息!?

2. 創建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;
}

編譯運行:

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函數獲取到的錯誤信息!?

二、perror

#include <stdio.h>
#include <errno.h>
void perror (const char *s); ? ?/* See NOTES */

s:

傳入參數,自定義的描述;

返回值:

無;

作用:

向標準出錯stderr 輸出錯原因 。

具體使用案例:

perror("create socket error ");

將需要提示的字符串傳參給perror函數即可,它相當于:

fprintf(stderr, "create socket error : ", strerror(errno));

perror是直接向出錯標準stderr輸出錯誤原因!

例:

#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;
}

編譯運行:

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函數輸出的錯誤信息!?

因為他是直接向出錯標準stderr輸出錯誤原因,我們沒法通過它獲得錯誤信息字符串,所以也就沒法將其寫入日志文件中!

最后,調用系統的任何函數,當出現了錯誤,都可以使用以上兩種方式進行打印輸出錯誤信息,從而更輕易的幫助我們找出對應問題!

如果需要將錯誤信息輸出到日志文件,使用strerror 函數;否則可以使用perror函數。

注意:以上介紹的 適用于任何Linux函數,例如open、write等。

原文鏈接:https://blog.csdn.net/cpp_learner/article/details/127825815

欄目分類
最近更新