系统编程错误处理

  在系统编程中错误通常通过函数返回值来表示,并通过特殊变量errno来描述。errno这个全局变量在<errno.h>头文件中声明如下:

    extern int errno;

  同时提供了两个错误处理函数:perror、strerror。

char * strerror(int errnum); //将错误码转换成文本信息。

  1 #include<unistd.h>
  2 #include<errno.h>
  3 #include<stdio.h>
  4 #include<string.h>
  5 int main()
  6 {
  7         int ret;
  8         ret=close(10);//10  fd
  9 //      if (ret==-1)
 10 //              perror("close  error");
 11         if (ret == -1)
 12                 fprintf(stderr,"close error with msg: %s
",strerror(errno))    ;
 13         return 0;
 14 }

  鉴于fprintf使用的不够熟练,下面简单复习一下fprintf函数、sprintf函数:https://blog.csdn.net/coolwriter/article/details/77868103

原文地址:https://www.cnblogs.com/wsw-seu/p/10832261.html