errno, strerror (errno)) 错误打印

每次调用失败的时候,系统会自动用用错误代码填充errno这个全局变量,

只需要读errno这个全局变量就可以获得失败原因

#include
#include
#include
static void open_device (int * fd, char * dev_name)
{
 *fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);

 if (-1 == *fd)
 {
  fprintf (stderr, "Cannot open '%s': %d, %s\n", dev_name, errno, strerror (errno));
  exit (EXIT_FAILURE);
 }
}

int main (int argc, char ** argv)

{

  char            *dev_name     = "/dev/video0";
   int                 fd                   = -1;

  open_device (fd, dev_name);

}

结果:Cannot identify '/dev/video0': 2, No such file or directory

你可能感兴趣的:(linux驱动)