Entangled with reading the structure of the file and placing them in memory. As I know, the compiler performs data alignment to increase performance. I am writing a program to read (read only) the file structure of fat16 (for informational purposes only). here is the structure itself
struct struct_fat16 { char Instruction[3]; char IdOs[8]; uint16_t BytesPerSector; uint8_t SectorPerCluster; uint16_t ReservedSectors; uint8_t NubmerOfFATs; uint16_t RootEntries; uint16_t TotalSectors; uint8_t MediaDescriptor; uint16_t SectorPerFat; uint16_t SectorPerTrack; uint16_t Heads; uint32_t HiddenSectors; uint32_t BigTotalSectors; uint8_t PhysicalDiskNumber; uint8_t CurrentHead; uint8_t Signature; uint32_t VolumeSerialNumber; char VolumeLabel[11]; char SystemID[8]; char CodeBootLoader[448]; char BootSiganture[2]; };
its size in memory is 520 bytes from me (although without alignment with the idea of 512). I use the following construction for reading:
struct struct_fat16 table; if (-1 == fread(&table, sizeof(struct_fat16), 1, f)) { ... }
but in this case, 520 bytes are read, not 512, and the data in the memory of the structure is also incorrectly placed. Therefore, the following question arises: is it possible to read data from a file and write it directly into the structure, that is, without using an intermediate buffer and copying each member:
char tmp[512]; fread(&tmp, 512, 1, f)); memcpy(&table.Instruction, tmp, 3); ...//так для каждого члена структуры memcpy(&table.BootSignature, tmp, 3);
How to calculate the size of the structure without alignment? Have to take the size of each of its members and summarize? Or do you have any other means?