   fread() and file headers
  I would like to write my own model file format and so I have been studying the various md2 and md3 loaders and I pretty much understand everything except for:fread(&m_Header, 1, sizeof(tMd3Header), m_FilePointer);where m_Header is a structure to contain the header info.I am sure it is somthing really easy that I am just overlooking or have forgotten about, but why does this work?!why do each of the member variables of m_header get the right data?This version confused me even more.buffer = (char*)malloc(fiLen + 1);fread(buffer, sizeof(char),fileLen,filePtr);m_Header = (modelHeader*)buffer;//?????!!!!where buffer is a character pointer.Does it just fill up the first member variable with data from the file and then when it''s full move on to the next and so on? 
quote:Original post by Stef3d  fread(&m_Header, 1, sizeof(tMd3Header), m_FilePointer);fread reads a specified number of bytes from a file into the memory buffer indicated as the first parameter. m_Header gets the right data in each of its members because to fread it''s simply a memory buffer. Every member has a size in bytes, so by reading the sum total number of bytes you "load" the entire structure at once.Caveat: Sometimes data structures get padded in C and C++ for byte-alignment. Consule your compiler documentation on how to alter this behavior.quote:m_Header = (modelHeader*)buffer;In this case, m_Header is a pointer (despite the name similarity). If it isn''t, the operation fails. Since the memory buffer contains the data of a modelHeader, casting buffer to modelHeader * causes the compiler to now view the memory buffer, via m_Header, as a modelHeader.Read up on your pointers. Hope that helped. ;  struct m_Header{int Something; // 4 bytesint Otherthing; // 4 bytes}Suppose that m_Header is 8 bytes, has 2 members that are each 4 bytes. This means that the first 4 bytes in m_Header belong to "Something" and the next 4 to "Otherthing". &m_header is an address that points to the start of m_Header. If you pass it to fread() and read 8 bytes starting at that pointer it''s no different than passing each member to fread() and reading 4 bytes at a time. This is because &Something points exactly to the same spot as &m_Header, and &Otherthing points to the 4 bytes away from those two.Now, as Oluseyi said, structs usually get padded for certain reasons, so don''t read structs with one fread() like that unless you want bugs, read each member seperately unless you are positive that your struct will not be padded.   ------------- outRider - ;quote:Original post by outRider   ...snip...Now, as Oluseyi said, structs usually get padded for certain reasons, so don't read structs with one fread() like that unless you want bugs, read each member seperately unless you are positive that your struct will not be padded.   ...snip...The first part is correct, the padding stuff is wrong (Unless you have two version of your app compiled in different compilers trying to read the saved data from the other version).When you save the struct with sizeof( struct ), even when it has padding, it will be saved with the padding. When you read it back it will be read with the padding again. Tada!!Real Caveat    : Objects with virtual tables cannot be saved and loaded like that. Since the virtual table is merely a pointer it will point to some wrong address after re-loading. [edited by - Endurion on February 12, 2004 3:24:35 AM];quote:Original post by Endurion   The first part is correct, the padding stuff is wrong (Unless you have two version of your app compiled in different compilers trying to read the saved data from the other version).When you save the struct with sizeof( struct ), even when it has padding, it will be saved with the padding. When you read it back it will be read with the padding again. Tada!!Real Caveat   : Objects with virtual tables cannot be saved and loaded like that. Since the virtual table is merely a pointer it will point to some wrong address after re-loading.Assuming he saved the struct himself with the same code+compiler+settings yeah, the padding should be the same. But really I was referring to reading vendor files saved by others, where you declare a struct to match the file contents and attempt to read it in one shot, where your struct is padded and doesn't align with the data in the file.   ------------- outRider -   [edited by - outRider on February 12, 2004 8:54:45 AM];quote:Original post by outRider  Assuming he saved the struct himself with the same code+compiler+settings yeah, the padding should be the same. But really I was referring to reading vendor files saved by others, where you declare a struct to match the file contents and attempt to read it in one shot, where your struct is padded and doesn''t align with the data in the file.   ------------- outRider -   Yeah right, i didn''t think of that use when i wrote that. My mistake.So Stef3d, outRider and i are both correct. Listen to us. Obey. Consume. Reproduce. Don''t ask questions. .... 