delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/03/03/17:03:50

Message-ID: <36DDBF84.438669BC@solutions2000.net>
Date: Wed, 03 Mar 1999 18:02:28 -0500
From: Nehru Juman <shadowcaster AT solutions2000 DOT net>
X-Mailer: Mozilla 4.04 [en] (Win95; I)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: (Problem with structs)
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 <iostream.h>
#include <fstream.h>
#include <conio.h>

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<<temp.one<<"  "<<temp.two<<endl;
     }

//   Here is where the problem starts.  This is supposed to change
element
//   6 of the array in the file.
     gDataFile.seekp(5*sizeof(alpha),ios::beg);
     gDataFile.write(&(beta[0]),sizeof(alpha));

//   When I read the file back I get funny output, shown below
     for(int c=0; c < 20; c++) {
        gDataFile.seekg(c*9);
        gDataFile.read(&temp,sizeof(alpha));
        cout<<temp.one<<"  "<<temp.two<<endl;
     }

     gDataFile.close();
}

This is the output I get:

10  abcd
20  efgh
30  ijkl                         <------All this is fine.
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
10  abcd                            <-----This is fine
20  efgh
30  ijkl
40  mnop
50  qrst
10  abcd                            <-----New array entry
2030043136  zxx               <-----Huh?
1879048192  oet
1711276032  ill
1711276032  old
1879048194  ink
1778384899  ail
1744830465  ope
1795162112  ing
1744830466  ell
1946157059  his
1660944387  lip
1778384935  ust
1711276032  ree
1929379841  lay

- Raw text -


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