views:

52

answers:

1

Hi,

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

Thank you all.

+2  A: 

"#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);
    }
}
clyfe
Thank you very much. One question, why does the open need the O_NONBLOCK flag?
IUnknownPointer
See here: http://opengroup.org/onlinepubs/007908799/xsh/open.html
clyfe
Hey, @clyfe, it doesn't work... ioctl returns always an error.
IUnknownPointer