www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/10/03/14:52:54

Message-Id: <199810031851.OAA30403@delorie.com>
Comments: Authenticated sender is <mert0407 AT sable DOT ox DOT ac DOT uk>
From: "George Foot" <george DOT foot AT merton DOT ox DOT ac DOT uk>
To: Sigurdur Smarason <ssmaraso AT emerald DOT tufts DOT edu>
Date: Sat, 3 Oct 1998 19:49:10 +0000
MIME-Version: 1.0
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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019