delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/04/17/11:39:19

Sender: tim AT riker DOT skynet DOT be
Message-ID: <3ADC6312.225AF8FD@falconsoft.be>
Date: Tue, 17 Apr 2001 17:36:50 +0200
From: Tim Van Holder <tim DOT vanholder AT falconsoft DOT be>
Organization: Falcon Software NV
X-Mailer: Mozilla 4.76 [en] (X11; U; Linux 2.2.16-3 i686)
X-Accept-Language: en, nl-BE, nl
MIME-Version: 1.0
To: Waldemar Schultz <schultzma AT ma DOT tum DOT de>
CC: djgpp AT delorie DOT com
Subject: Re: MINGW vs DJGPP
References: <Pine DOT SUN DOT 3 DOT 91 DOT 1010417165008 DOT 15278J-100000 AT is> <9bhhqg$bfo$1 AT talia DOT mad DOT ttd DOT net> <3ADC5345 DOT 8D25094F AT ma DOT tum DOT de> <3405-Tue17Apr2001180243+0300-eliz AT is DOT elta DOT co DOT il> <3ADC5E71 DOT 764DF1E6 AT ma DOT tum DOT de>
Reply-To: djgpp AT delorie DOT com

Waldemar Schultz wrote:
> 
> Eli Zaretskii schrieb:
> > You could probably make it work with the `-fpack-struct' switch, but
> > I'd suggest not to try: it's a futile exercise.
> >
> > Either you need to read this struct from a program compiled by a
> > different compiler than the one used to write the struct, or you
> > don't.  If you do need this, the best way is to read the data as a
> > byte stream into a buffer, then unpack the buffer into the struct
> > manually.  But the best alternative is to read and write the file by
> > programs compiled with the same compiler.
> 
> That's what I was afraid of. so I have to port all that huge project,
> data generation as well as data visualization - sigh.
> But thanks for the instant reply anyway.
> 

If you're only uing DJGPP and MINGW32 you could use a gcc extension
to pack just this structure:

struct _foo {
  int a;
  char b;
  ...
}  foo ;

It _may_ be needed to add the '__attribute__((packed))' after each field
as well, I don't remember what I used to do (I now compile my gcc with
the #pragma pack (a la Visual C) enabled).

Alternatively, you could try to reorder the fields of the structure
so that the padding issue does not arise; but that may not be possible,
and if it is would require a lot of fiddling.

But you seem to have a bigger problem: you seem to read/write an array
of
structures containing pointers from/to file.  Unless you restore the
contents during a single program run (which, since you use the data
files
in two separate programs, seems not to be the case), that means those
pointers would point into nothingness after a restore.
So using a streamier approach seems to be the only viable solution.
But since the pointers in your example seem to be strings, you could
probably get away with just dumping the structure, and then dumping both
strings after it.  You could store the string's length in the pointer
member to save room:

void
write_struct(VP* obj, FILE* f)
{
char* name = obj->nam;
char* beschreibung = obj->dsc;
  obj->nam = (char*) strlen(name);
  obj->dsc = (char*) strlen(beschreibung);
  fwrite (obj, sizeof (*obj), 1, f);
  fwrite (name, (int) obj->nam, 1, f);
  fwrite (beschreibung, (int) obj->dsc, 1, f);
  obj->nam = name;l
  obj->dsc = beschreibung;
}

void
read_struct(FILE* f, VP* obj)
{
int len = 0;
  fread (obj, sizeof (*obj), 1, f);
  len = (int) obj->nam;
  obj->nam = (char*) malloc(len + 1);
  fread (obj->nam, len, 1, f);
  obj->nam[len] = '\0';
  len = (int) obj->dsc;
  obj->dsc = (char*) malloc(len + 1);
  fread (obj->dsc, len, 1, f);
  obj->dsc[len] = '\0';
}

Disclaimer: untested, off the top of my head, so nu guarantees this'll
work as is.

-- 
Tim Van Holder - Falcon Software N.V.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
This message was posted using plain text. I do not endorse any
products or services that may be hyperlinked to this message.

- Raw text -


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