Message-ID: <36DDBF84.438669BC@solutions2000.net> Date: Wed, 03 Mar 1999 18:02:28 -0500 From: Nehru Juman X-Mailer: Mozilla 4.04 [en] (Win95; I) MIME-Version: 1.0 To: djgpp AT delorie DOT com Subject: (Problem with structs) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Reply-To: djgpp AT delorie DOT com Hi Everybody, I'm having a problem with a program I took directly from a c++ book ("A Beginners C++) which involves writing structs to disk. Since it comes from the book I assume it should work. I wrote a simplified program to isolate the problem. Can anybody tell me why the below code doesn't work properly: // Program to write an array of structs to disk and read it back #include #include #include typedef struct { int one __attribute__ ((packed)); char two[5] __attribute__ ((packed)); } alpha; alpha beta[20]={{10,"abcd"},{20,"efgh"},{30,"ijkl"}, {40,"mnop"},{50,"qrst"},{60,"uvwx"}, {70,"yzxx"},{80,"poet"},{90,"fill"}, {200,"fold"},{700,"pink"},{900,"jail"}, {333,"hope"},{111,"king"},{666,"heck"}, {777,"this"},{1000,"clip"},{9999,"just"}, {101,"free"},{333,"slay"}}; alpha temp={99,"nope"}; fstream gDataFile; int main() { gDataFile.open("testfile.my",ios::in|ios::out); // This writes the array to the file for(int a=0; a < 20; a++) { gDataFile.seekp(a*sizeof(alpha),ios::beg); gDataFile.write(&(beta[a]),sizeof(alpha)); } // This read the array from file and displays the structure data for(int b=0; b < 20; b++) { gDataFile.seekg(b*sizeof(alpha),ios::beg); gDataFile.read(&temp,sizeof(alpha)); cout<