zoukankan      html  css  js  c++  java
  • fprintf 和 perror 的理解1

    程序中的两种出错处理:

    第一种: 用fprintf

       2:  #include <string.h>
       3:  #include <errno.h>
       4:  #include <stdlib.h>
       5:   
       1:  #include <stdio.h>
       6:  int main(void)
       7:  {
       8:      FILE *fp;
       9:   
      10:      if((fp = fopen("1.c", "r")) == NULL)
      11:      {
      12:          fprintf(stderr, "fopen error! %s", strerror(errno));
      13:          exit(-1);
      14:      }
      15:      return 0;
      16:  }

    image

    可以看出,这种输出结果不会自动换行。而且参数较多。

    第二种、perror

       1:  #include <stdio.h>
       2:  #include <stdlib.h>
       3:   
       4:  int main(void)
       5:  {
       6:      FILE *fp;
       7:   
       8:      if((fp = fopen("1.c", "r")) == NULL)
       9:      {
      10:          perror("fopen error!");
      11:          exit(-1);
      12:      }
      13:      return 0;
      14:  }

    image

    可以看出:用perror的输出会自动换行,且自动加上一个冒号和错误提示。推荐!

  • 相关阅读:
    Maven依赖
    Maven坐标
    初识Maven POM
    Maven配置
    相似文本文档分析之SimHash算法
    Ubuntu14.10下JDK编译安装详细操作说明
    Ubuntu14.10下Tomcat编译安装 详细操作说明
    ubuntu 14.10 编译安装 Ruby
    ubuntu 14.10 编译安装 Python
    ubuntu 14.10 编译安装 Golang
  • 原文地址:https://www.cnblogs.com/pengdonglin137/p/2952414.html
Copyright © 2011-2022 走看看