Hi,
How can I get the sector size for floppy and CD disks in Linux, via C++ code?
Thank you all.
Hi,
How can I get the sector size for floppy and CD disks in Linux, via C++ code?
Thank you all.
"#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);
}
}