linux perror()函数

来源:互联网 发布:php numericvalue 编辑:程序博客网 时间:2024/05/10 08:30

    头文件:  

      #include<stdlib.h> perror是包含在这个文件里;


函数定义:

  void perror(const char *s); 

函数说明

  perror ( )用 来 将 上 一 个 函 数 发生 错 误 的 原 因 输 出 到 标 准 设备 (stderr) 。参数 s 所指的字符串会先打印出,后面再加上出错原因字符串。此错误原因依照全局变量error 的值来决定要输出的字符串。在库函数中有个error变量,每个error值对应着以字符串表示的错误类型,可以利用strerror(int)函数将出错字符串信息打印出来。当你调用"某些"函数出错时,该函数已经重新设置了error的值。perror函数只是将你输入的一些信息和现在的error所对应的错误一起输出。
#include <stdio.h>#include <errno.h>#include <stdlib.h>int main(){    FILE* fd;    fd = fopen("/mnt/hg/hello","r");    if(NULL == fd)    {        perror("can not open file");        return -1;    }    return 0;}     
运行结果:can not open file :No such file or directory.

errno - number of last error 

errno 记录系统的最后一次错误代码。代码是一个int型的值,在errno.h中定义。




0 0
原创粉丝点击