linux下的strerror和perror
生活随笔
收集整理的這篇文章主要介紹了
linux下的strerror和perror
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? ? ? ?strerror和perror用于獲取error相關的錯誤信息,strerror接受一個int,perror接受一個字符串。
#include <stdio.h> #include <errno.h> #include <string.h>int main(int argc,char *argv[]) {printf("%s\n",argv[0]);errno=2;printf("%s\n",strerror(errno));perror(argv[0]);return 0; }編譯運行: [mapan@localhost APUE]$ ./a.out ./a.out No such file or directory ./a.out: No such file or directory argv[0]=./a.out,把上述代碼中的argv[0]換成argv[1],編譯并運行。 [mapan@localhost APUE]$ ./a.out 1111 1111 No such file or directory 1111: No such file or directory這證明argv[1]只表示輸出前面指定的參數,與錯誤碼無關。perror()輸出的錯誤只與errno有關,與傳入的參數無關。
總結
以上是生活随笔為你收集整理的linux下的strerror和perror的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实战测试SO_REUSEADDR选项
- 下一篇: linux 其他常用命令