How to find floppy\ CD sector size in Linux?

2019-04-13 00:40发布

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

Thank you all.

1条回答
做个烂人
2楼-- · 2019-04-13 01:29

"#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);
    }
}
查看更多
登录 后发表回答