Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!spool.mu.edu!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!rearl From: rearl@gnu.ai.mit.edu (Robert Earl) Newsgroups: comp.lang.c Subject: Re: Little problem with sizeof on PC Message-ID: Date: 23 Apr 91 08:02:30 GMT Article-I.D.: nutrimat.REARL.91Apr23040230 References: <1991Apr23.022057.29511@ux1.cso.uiuc.edu> <1991Apr23.050747.19705@agate.berkeley.edu> Sender: news@ai.mit.edu Organization: (EVIL!) Lines: 26 In-reply-to: c60b-1eq@e260-1d.berkeley.edu's message of 23 Apr 91 05:07:47 GMT In article <1991Apr23.050747.19705@agate.berkeley.edu> c60b-1eq@e260-1d.berkeley.edu (Noam Mendelson) writes: | In article <1991Apr23.022057.29511@ux1.cso.uiuc.edu> allender@ux1.cso.uiuc.edu (Mark Allender) writes: | | >Now, I want to read the beginning of a binary file into this structure, | >so I do something like this: | > struct header Header; | > if ((readnum = read(fd, (char *)(&Header), sizeof(Header))..... | | Hmmm. That should be: | if ((readnum = read(fd, (struct header *)(&Header), sizeof(Header)) ... Nope, (char *) &Header is right, because "&Header" is already a "struct header *" and read() expects a "char *" argument. | The best thing would be to restructure the data file, using 202 byte blocks | instead of 201 to be on the safe side. At worst, you're increasing the | size of the data file by about .5%. If it were just a data file, the best thing would be to make it a plain text file and use fscanf() or fgets(), but he appears to want only the header bit of it, so he should just be very careful to note this binary representation could break under another operating system/CPU/phase of the moon. --robert