zoukankan      html  css  js  c++  java
  • Linux C函数之错误处理函数

    转:

    错误处理函数(3)

    ferror: 检查文件流是否有错误发生
    头文件: stdio.h
    函数定义: int ferror(FILE *stream);
    说明: ferror()用来检查参数stream所指定的文件流是否发生了错误情况, 若有则返回非0值.

    perror: 打印出错误原因信息字符串
    头文件: stdio.h
    函数定义: void perror(const char *s);
    说明: perror()用来将上一个函数发生错误的原因输出到标准错误(stderr). 参数s所指的字符串会先打印出, 后面加上错误的原因字符串. 此错误原因依照全局变量errno的来决定要输出的字符串.
    应用举例:
    #include <stdio.h>
    int main(void)
    {
    FILE *fp;
    fp = fopen("/tmp/fdsafda", "r+");
    if(fp == NULL)
    {
       perror("fopen");
    }
    return 0;
    }
    运行结果:
    fopen: No such file or directory

    strerror: 返回错误原因的描述字符串
    头文件: string.h
    函数定义: char *strerror(int errnum);
    说明: strerror()用来依参数errnum的错误代码来查询错误原因的描述字符串, 然后将该字符串指针返回. 通常给其传递全局变量errno.
    应用举例:
    #include <stdio.h>
    #include <errno.h>
    int main(void)
    {
    char *buffer;
    buffer = strerror(errno);
    printf("Error: %s\n", buffer);
    return 0;
    }
    运行结果:
    Error: Success

  • 相关阅读:
    LeetCode 449. Serialize and Deserialize BST
    LeetCode Word Abbreviation
    LeetCode 402. Remove K Digits
    LeetCode 439. Ternary Expression Parser
    LeetCode Frog Jump
    LeetCode 630. Course Schedule III
    LeetCode 729. My Calendar I
    LeetCode 567. Permutation in String
    LeetCode Find Permutation
    LeetCode Number of Atoms
  • 原文地址:https://www.cnblogs.com/hcu5555/p/2659943.html
Copyright © 2011-2022 走看看