c++ - Reading Superblock into a C Structure -
I have a disk image that has a standard image using fuses. The superblock has the following, and I have a function read_superblock (* buf) which gives the following raw data:
byte 0-3: magic number (0xC0000112) 4-7: block size ( 1024) 8-11: Total file system size (in block) 12-15: FAT Length (in block) 16-19: Root directory (block number) 20-1023: Not used
I am very new to C and am eager to start on this project that it is easy to read in a structure or some variable Rica, and can print them on the screen using printf for debugging.
I was initially thinking that I could see raw data just as I was thinking of doing something, but I think this is not the case. There is also no structure and I am trying to read it as a string which is very wrong. Is there a way to specify the structure for me and to define the number of bytes in each variable to take out data for me?
char * buf; Read_superblock (* buf); Printf ("% s", buf);
Yes, I think it would be better to read it in a composition. Fields with useful data are all 32-bit integers, so you can define the structure that looks like this (by using the defined values in the standard header file
stdint.h
):typingfix straight superblock_street {uint32_t magic_number; Uint32_t block_size; Uint32_t fs_size; Uint32_t fat_length; UIT 32_TY Root_Dir; } Superblock_t;
You can call
read_superblock
to callchar *
, like this:< Code> superblock_tb; Read_superblock ((* four *) and SB);To print your data now, you can do the following like a call:
printf ("% d% d% d% d \ N ", Sb.magic_number, sb.block_size, sb.fs_size, sb.fat_length, sb.root_dir);
Note that you should be aware of the end of your platform while using this kind of technique, because you are reading the integer data (i.e., to read your data You may need to swap the bytes during). You should be able to decide first using the magic number in the field quickly.
Note that it will generally be better to keep it close without any structure; This allows you to take advantage of the compiler's type-probe and remove potential problems that can hide casting. However, it will need to change the implementation of
read_superblock
to read data directly into a structure. It is not difficult and can be done using the standard C runtime functionfread
(assuming your data is in a file, as indicated in your question), such as: < / P>Fred (& sb.magic_number, sizeof (sb.magic_number), 1, fp); Fred (& sb.block_size, sizeof (sb.block_size), 1, fp); ...
Comments
Post a Comment