⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 实例13-3.c

📁 Linux下C语言编程例子
💻 C
字号:
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/cdrom.h>
/* CD驱动器对应的设备文件 */
#define DEVICE "/dev/cdrom"

int main() 
{
  int fd;
  int status;
  int number;
  int first_track, last_track;
  struct cdrom_tochdr header;

  /* 打开设备 */
  fd = open(DEVICE, O_RDONLY);
  if (fd < 0) {
    perror("unable to open " DEVICE);
    exit(1);
  }
  /* 读取CD头部信息 */
  status = ioctl(fd, CDROMREADTOCHDR, &header);
  if (status != 0) {
    perror("CDROMREADTOCHDR ioctl failed");
    exit(1);
  }
  /* 计算音轨数目 */
  first_track = header.cdth_trk0;
  last_track = header.cdth_trk1;
  number = last_track;
  printf("This CD has %d tracks.\n", number);
  /* 关闭设备 */
  status = close(fd);
  if (status != 0) {
    perror("unable to close " DEVICE);
    exit(1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -