C语言学习之输入/输出函数第一讲

1.perror
    头文件
    #include<stdio.h>
    #include<stdlib.h>
    注意:
        不可以掉了#include<stdio.h>这个头文件,perror是包含在这个文件里的
perror表头文件完善版定义函数
    函数原型:void perror(const char *s); 

              perror ("open_port");
    函数说明:
            perror()用来将上一个函数发生错误的原因输出到标准设备(stderr)。参数 s 所指的字符串会先打印出,后面再加上错误原因字符串。此错误原因依照全局变量errno 的值来决定要输出的字符串。在库函数中有个errno变量,每个errno值对应着以字符串表示的错误类型。当你调用"某些"函数出错时,该函数已经重新设置了errno的值。perror函数只是将你输入的一些信息和现在的errno所对应的错误一起输出。
    范例
    #include <stdio.h>
    int main(void)
   {
      FILE *fp ;
      fp = fopen( "/root/noexitfile", "r+" );
      if ( NULL == fp )
      { 
         perror("/root/noexitfile");
      }
      return 0;
    }
运行结果
[root@localhost io]# gcc perror.c
[root@localhost io]# ./a.out
/root/noexitfile: No such file or director


2.perror函数和strerror函数的区别

首先简单说一下file descriptors(文件描述符)

      file descriptor 0是standard input (stdin标准输入)

      file descriptor 1 是 standard output (stdout标准输出) 

      file descriptor 2 是 standard error output(stderr标准错误输出)

perror()原型:

      #include <stdio.h>

      void perror(const char *msg);

      它是基于errno的当前值,在标准出错上产生一条出错信息,然后返回。它首先输出由msg指向的字符串,然后是一个冒号,一个空格,接着是对应于errno值的出错信息,最后是一个换行符。

strerror()原型:

      #include <string.h>

      char * strerror(int errnum);
此函数将errnum(它通常就说errno值)映射为一个出错信息字符串,并返回此字符串的指针。

      perror是将errno对应的错误消息的字符串打印到标准错误输出上,即stderr或2上,若你的程序将标准错误输出重定向到/dev/null,那就看不到了,就不能用perror了。而 strerror的作用只是将errno对应的错误消息字符串返回,要怎样处理完全由你自己决定。通常我们选择把错误消息保存到日志文件中,即写文件,所以通常可以用fprintf(fp, "%s", strerror(errno))将错误消息打印到fp指向的文件中。其中perror中errno对应的错误消息集合跟strerror是一样的,也就是说不会漏掉某些错误。

例子:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>

int main(int argc,char *argv[])
{
        FILE * fp;

        if((fp = fopen(argv[1],"r"))==NULL)
        {
                perror("perror");
                printf("fork error:%s\n",strerror(errno));
                exit(1);
        }
        perror("perror");
        printf("fork error:%s\n",strerror(errno));

        return 0;
}
结果:

zjf@zjf:~/work/code/procise/application/chapter_1$ ./a.out 
perror: Bad address
fork error:Bad address
zjf@zjf:~/work/code/procise/application/chapter_1$ ./a.out 2> file
fork error:Invalid argument
例子:

#include <stdio.h> // void perror(const char *msg);

#include <string.h> // char *strerror(int errnum);

#include <errno.h> //errno

errno是错误代码,在errno.h头文件中

void perror(const char *s)

perror是错误输出函数,在标准输出设备上输出一个错误信息。

参数s一般是参数错误的函数

例如perror("fun"),其输出为:fun:后面跟着错误信息(加上一个换行符)

char *strerror(int errnum);通过参数errnum(也就是errno),返回错误信息

以下是测试程序:

//程序名:errtest.c,环境为linux

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(int argc,char *argv[]){
FILE *fp;
char *buf;
if((fp=fopen(argv[1],"r"))==NULL)
{
perror("perror");
printf("sterror:%s\n",strerror(errno));
exit(1);
}
perror("perror");
errno=13;
printf("strerror:%s\n",strerror(errno));
fclose(fp);
return 0;
}

==============================

编译为errtest

如果输入这样的命令格式:./errtest 111.c(其中111.c不存在)

输出为:

perror: No such file or directory
sterror:Illegal seek

就是两个都是输出到屏幕上来了。而且sterror函数通过errno得到错误代码

如果命令格式为:./errtest 111.c > out.c(其中111.c不存在)

把输出重定位到out.c文件中,会发现屏幕输出为:

perror: No such file or directory
就是说函数perror始终输出到标准输出设备上。而printf输出到文件中了



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值