linux系统调用出错处理perror()

1.haha.log不存在


2.我们试图打开haha.log

                           
 #include<stdio.h>          
 #include <sys/types.h>     
 #include <sys/stat.h>      
 #include <fcntl.h>         
  #include <errno.h>

         
 int main(int argc,char**argv)
  {                          
      int fd;                
      fd=open("haha.log",O_RDONLY);//以只读形式打开一个不存在的文件,fd=-1
      if(fd<0)               
      {                      
      perror("open failure"); //系统调用出错处理,写XXX失败,调用perror后会自动在后加:错误原因                                                
      //printf("open failure:%s\n",strerror(errno));
       return 0;             
      }                      
  }                          
      

3.运行结果:

[luoyiran@luoyiran ~]$ ./love
open failure: No such file or directory

猜你喜欢

转载自blog.csdn.net/luoyir1997/article/details/82694646