2010-06-27 63 views

回答

2

"#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); 
    } 
} 
+0

非常感謝。一個問題,爲什麼open需要O_NONBLOCK標誌? – IUnknownPointer 2010-06-28 08:20:05

+0

請參閱:http://opengroup.org/onlinepubs/007908799/xsh/open.html – clyfe 2010-06-28 10:36:36

+0

嘿,@clyfe,它不起作用... ioctl總是返回錯誤。 – IUnknownPointer 2010-07-19 09:43:48