Message-Id: <199810031851.OAA30403@delorie.com> Comments: Authenticated sender is From: "George Foot" To: Sigurdur Smarason Date: Sat, 3 Oct 1998 19:49:10 +0000 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: Re: newbie:problem with fread Reply-to: mert0407 AT sable DOT ox DOT ac DOT uk CC: djgpp AT delorie DOT com X-mailer: Pegasus Mail for Win32 (v2.42a) On 3 Oct 98 at 15:55, Sigurdur Smarason wrote: > Now if I call each seperately as in: > > size=fread(&header.norm_offset,sizeof(header.norm_offset),1,data); > size=fread(&header.extra_records,sizeof(header.extra_records),1,data); > size=fread(&header.num_records,sizeof(header.num_records),1,data); > etc. > > it works as it should, but this is rather clumsy in my opinion, is there a > better way ? Type at a DOS prompt: info gcc "C Extensions" "Type Attributes" Look in particular at the `packed' attribute. However, note that using this attribute will slow down access to the fields of the struct, and slow down accessing arrays of the struct. It's also not portable to other compilers. IMHO you ought to leave the struct aligned, and do something like what you just mentioned above. If portability is a real concern (i.e. if you want to be able to read back your files on other machines or using other compilers) then you'll have to write the fields of your struct byte by byte -- you need to decide how many bytes wide each field is, and write that number of bytes, in a standard format across all machines and compilers that will need to read the code. Decide on an endianness, signedness and field width, and stick to it. For non-integer types such as `float' you'll need another format, of course -- don't try to extract bytes from a floating point number. IMHO it's sensible to do this even if you don't intend to port the program to other platforms, just in case you change your mind. -- george DOT foot AT merton DOT oxford DOT ac DOT uk