日期:2014-05-16  浏览次数:20540 次

编程遇到错误,百思不得其解,请指教
这是一个弹出光驱的程序,有三个模块组成,把打开设备做成函数便于其他程序的调用,但是不知道哪里出了问题,在运行时总是提示   CDROMEJECT   ioctl   failed:   Bad   file   descriptor   ,不知该如何解决,望高人赐教

/*function,h*/
#ifndef   _FUNCTION_H
#define   _FUNCTION_H

#include   <unistd.h>
#include   <stdlib.h>
#include   <stdio.h>
#include   <fcntl.h>
#include   <sys/ioctl.h>

#include   <linux/cdrom.h>

int   open_device();

/*   CD驱动器对应的设备文件   */

#define   DEVICE   "/dev/cdrom "

#endif


/*function.c*/
#include   "function.h "
int   open_device()
{  
    int   fd;
    fd   =   open(DEVICE,   O_RDONLY);

    if   (fd   <   0)   {

      perror( "unable   to   open   "   DEVICE);

        exit(1);

    }
return   fd;
}

/*eject.c*/
#include   "function.h "

int   main()

{

    int   fd;

    int   status;

    /*   打开设备   */

    open_device();

    /*   退出光盘   */

    status   =   ioctl(fd,   CDROMEJECT);

    if   (status   !=   0)   {

        perror( "CDROMEJECT   ioctl   failed ");

        exit(1);

    }

    /*   关闭设备   */

    status   =   close(fd);

    if   (status   !=   0)   {

        perror( "unable   to   close   "   DEVICE);

        exit(1);

    }
return   0;

}


然后运行   gcc   function.c   eject.c   -o   eject
./eject
即提示     CDROMEJECT   ioctl   failed:   Bad   file   descriptor
不知如何解决?


------解决方案--------------------
fd = open_device();

------解决方案--------------------
open_device();
status = ioctl(fd, CDROMEJECT);//?
呵呵
你是否真是那样?
fd = open_device();
------解决方案--------------------
status = ioctl(fd, CDROMEJECT);//这里fd没有赋值,系统不知道你要做什么