2

我正在编写一个程序,我需要访问引导扇区中有关已挂载的 FAT32 文件系统的一些信息。

这是我所做的,完全评论。

int main (void) {
    unsigned short *i;                  //2 byte unsigned integer pointer
    char tmp[1024];                     //buffer
    int fd;                             //file descriptor from opening device
    fd =  open("/dev/sdf", O_RDONLY);   //open the device file
    lseek(fd, 14, SEEK_SET);            //set offset to 14 (0x0E), i.e. storing "no. of reserved sectors" of the FAT32 system
    read(fd, tmp, 2);                   //read off 2 bytes, "no. of reserved sectors" is represented by 2 bytes
    i = &tmp;                           //point j at those 2 bytes
    printf("*j: %d\n", *j);             //print *j out
    close(fd);                          //close device
    return 0;
}

*i 的输出是 38,这是胡说八道。我用 mkfs.vfat 格式化了文件系统。我将“保留扇区数”设置为 32。

我试过的:

  • i = (unsigned short *) &tmp,进行强制转换,这会在我编译时消除警告,但无济于事

  • read(fd, tmp, 512),将整个引导扇区(512 字节)加载到 tmp 中,然后从缓冲区中读取,但没有帮助,结果仍然是 38。

  • 摆弄偏移量,即将 14 更改为 13 或 15,以防我弄错索引。它分别为 13 和 15 打印 9744 和 512,因此不起作用。

我不确定我的做法是否正确。有人可以指出我正确的方向吗?

提前致谢,

菲拉斯汀。

4

2 回答 2

0

尝试运行:

$ dd if=/dev/sdf of=/tmp/x.bin bs=512 count=1

接着:

$ hd /tmp/x.bin

或者

$ od -tx2 /tmp/x.bin

并发布第一行。

您的胖工具很有可能会添加 6 个额外的保留扇区。然后他们在显示数据之前减去它们。

于 2011-12-21T14:37:57.723 回答
-1
unsigned short *i;                  //2 byte unsigned integer pointer
char tmp[1024];  
 [...] 
i = &tmp;                           //point j at those 2 bytes

tmpchar[]&tmp有秩序的东西char**。再想想,你不想要&这里。

于 2011-12-22T07:36:57.510 回答