open返回值-1是什么原因

今天在调试代码的时候出现了一个问题 

给出代码的例子来说明这个问题

代码如下:

#define DEV_NAME  "/dev/sunxi-reg"
g_alloc_context->fd = open(DEV_NAME, O_RDWR,0);
if (g_alloc_context->fd <= 0)
{
	LOGE("open %s failed g_alloc_context->fd = %d\n", DEV_NAME,g_alloc_context->fd);
	goto ERROR_OUT;
}


执行之后的效果如下:

于是用这种方式进行调试 

代码如下:

#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#define DEV_NAME        "/dev/sunxi-reg"
int main(void)
{
        int flag=0;
        flag = open(DEV_NAME,O_RDWR,0);
        printf("%d  %s\n",flag,strerror(errno));
        return 0;
}


这次生成的可执行文件运行效果如下:


可以看到这个没有这个设备 当open返回值为-1.

那么看看例外一种情况 成功打开的时候

#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#define DEV_NAME        "/dev/disp"


int main(void)
{
        int flag=0;
        flag = open(DEV_NAME,O_RDWR,0);
        printf("%d  %s\n",flag,strerror(errno));
        return 0;
}


运行的效果如图:


但是在系统下面确实有


这个里面到底是哪里的问题,那么可以判断应该是 linux设备驱动没有做好。

调试的时候能用strerror这种系统函数是非常方便快捷查到哪里出了问题。

所以后面要从驱动上入手。

猜你喜欢

转载自blog.csdn.net/u011046042/article/details/64130787
今日推荐