转:perror和strerror的区别

概述:

perror和strerror都是C语言提供的库函数,用于获取与erno相关的错误信息,区别不大,用法也简单。最大的区别在于perror向stderr输出结果,而 strerror向stdout输出结果。

测试代码如下:

 

[cpp] view plain copy
 
 print?
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <errno.h>  
  4.   
  5. int main(int argc, char* argv[])  
  6. {  
  7.     FILE *fp;  
  8.     if ((fp = fopen(argv[1], "r")) == NULL)  
  9.     {  
  10.         perror("perror:");  
  11.         printf("strerror:%s ", strerror(errno));  
  12.     }  
  13.     exit(0);  
  14. }  



 

运行结果:

原文地址:https://www.cnblogs.com/yfz0/p/5820974.html