How to find floppy\ CD sector size in Linux?

旧街凉风 提交于 2019-12-06 11:36:04

问题


How can I get the sector size for floppy and CD disks in Linux, via C++ code?

Thank you all.


回答1:


"#include <hdreg.h>" and use ioctl HDIO_GET_IDENTITY to obtain a struct hd_driveid.
On this structure, the x->sector_bytes field is the sector size.

#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <cctype>
#include <unistd.h>

int main(){
    struct hd_driveid id;
    char *dev = "/dev/hdb";
    int fd;

    fd = open(dev, O_RDONLY|O_NONBLOCK);
    if(fd < 0) {
        perror("cannot open");
    }
    if (ioctl(fd, HDIO_GET_IDENTITY, &id) < 0) {
        close(fd);
        perror("ioctl error");
    } else {
        close(fd);
        printf("Sector size: %du\n", id.sector_bytes);
    }
}


来源:https://stackoverflow.com/questions/3127119/how-to-find-floppy-cd-sector-size-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!