【发布时间】:2013-11-18 23:35:19
【问题描述】:
现在,我正在编写一个示例代码,希望稍后将其集成到我的程序中。基本上我要做的是逐字节读取 .dat 文件并解释数据(即解释引导扇区以输出扇区大小、保留扇区等)
为此,我逐字节读取数据,并使用 https://www.win.tue.nl/~aeb/linux/fs/fat/fat-1.html#ss1.3 的 fat12 中的描述,将数据转换为我想要的信息。现在,我可以从文件中提取单个字节(假设提取的数据是十六进制的是否正确?)。但是,我需要两个字节才能有意义。所以,我需要将两个字节合二为一,将十六进制数据转换为十进制并输出信息。不幸的是,现在,我遇到了段错误,对于我的生活,我无法弄清楚出了什么问题。提前致谢!
int main (int argc, char **argv){
FILE *fp ,*fptest;
long lSize;
char *buffer;
//Open file
fptest= open("fat_volume.dat", "rb");
//Read file into buffer
fread(buffer,1,512,fptest);
//Parse the boot sector
char tmpA, tmpB;
tmpA = buffer[10]; //First byte
tmpB = buffer[11]; //Second byte
//Combine the two bytes into one
char combinedBytes[3];
strcpy (combinedBytes, tmpA);
strcat (combinedBytes, tmpB);
//Hex to decimal converter
long int li1;
li1 = strtol (combinedBytes,NULL,16);
printf ("The sector size is: %ld.\n", li1);
return 0;
}
【问题讨论】:
-
你永远不会
malloc为buffer提供一个数组。一路走来,你需要说类似buffer = malloc(512); -
你能解释一下吗?我的C非常生锈。一个例子将不胜感激!
-
您将自己定义为
char*,但您从不将其指向任何东西。它可能指向某个未知位置,如果您尝试访问它,可能会打开一个虫洞。您需要将其设置为指向您拥有的一些内存。
标签: c