如何通过C ++代码获得Linux中软盘和CD盘的扇区大小?
谢谢你们。
"#include <hdreg.h>"
并使用ioctl HDIO_GET_IDENTITY
来获得struct hd_driveid
。
在这个结构中, x->sector_bytes
字段是扇区大小。
#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); } }