转:perror和strerror的区别

时间:2023-03-09 01:05:06
转:perror和strerror的区别

概述:

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

测试代码如下:

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

运行结果:

转:perror和strerror的区别