如何通过 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);
}
}