Date: Wed, 26 Jun 1996 08:23:37 +0200 (IST) From: Eli Zaretskii To: j DOT aldrich6 AT genie DOT com Cc: djgpp AT delorie DOT com Subject: Re: Help -- Reading PCX Header In-Reply-To: <199606260356.AA228101373@relay1.geis.com> Message-Id: Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII On Wed, 26 Jun 1996 j DOT aldrich6 AT genie DOT com wrote: > If you don't want to bother reading all that, the way to fix your problem > is to change your struct definition as follows: > > typedef struct { > .... > } __attribute__((packed)) pcxHead; > Note that this is still not enough to ensure that the structure will be read from a file without breaking a program, because (AFAIK) this doesn't prevent the compiler from padding the struct *at the end* (in case somebody makes an array of such structs). If that happens, there will be no holes between the fields, but the size of the struct (as returned by sizeof) will still be larger than the sum of the sizes of all the fields, so passing that size to `read' might still cause trouble. The only way to make this always work is to read the portion of the file into a char of suitable length, then fill the struct fields one by one. If you do this, you don't need to make the struct packed, so you don't pay the runtime penalty. (Sometimes making the program correct also makes it faster ;-).