Re: Fwd: RE: [Dev-C++] [Dev-C++]: Errors in structures
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Elias P. <eli...@ao...> - 2001-07-04 13:25:52
|
... > Now if this is writen to the File we do it as > > fwrite(&oHeader,sizeof(oHeader),1,outfile); > > Meaning the size that we have occrding to our compiler.,., > > NOW -- we go and write a App to read this in DevCpp and we have the same > structure BUT > DevCpp (or the gcc or mingW) has a different value of the sizeof() so it > will end up as follows > we read the file in > > fread(&oHeader,sizeof(oHeader),1,infile); > > and we expect the result to look the same. BUT the sizeof this program > returned 24 forinstance and > we will start reading the beginning of the next entry of the second > structure... > > Now this will make my example more true.. > > I hope I understand this incorrect and that somebody can please explain > this problem to me in more detail... You understand correct: it is almost always a bad idea to do something like fread(&oHeader,sizeof(oHeader),1,infile); // bad ! If you want to store data into a file, and there is any chance this file might be read by another application, or the same application, but in a different compile, don't do this. Use an existing file format, or create your own instead. It should exactly specify how the data are stored. Then both applications for reading and writing can look at the specifications and read/write it accordingly. If you still want to use fread/fwrite, try using this with gcc, as i explained in a previous mail: __attribute__((packed)) Elias Pschernig |