delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/12/31/11:47:31

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Why does DJGPP's getw not read Borlands getw?
Date: Wed, 31 Dec 1997 11:43:11 -0500
Organization: Two pounds of chaos and a pinch of salt.
Lines: 43
Message-ID: <34AA761F.1C2E@cs.com>
References: <34A96539 DOT 424E AT ns DOT sympatico DOT ca>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp219.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Dave Nugent wrote:
> 
> Can someone explain to me why I have a file that was created in Borlands
> Turbo C++ v3.0  that wrote a bunch of integers can't be read properly in
> DJGPP?
> 
> Is there a difference in getw ??  I've checked the code over and can't see
> anything wrong?  Is it because DJGPP has a different int size than
> Borlands compiler?
> If so, is there anything I can do to read my old files?
> I have tried the getc and that seems to work fine.

Integers in DJGPP are 32-bit, while Borland integers are 16-bit.  getw()
and putw() read/write the native int size for the compiler they are used
on.  A better option would be to use short integers (16-bit) in your
DJGPP programs and use fread() to read them in.  fread() allows you to
explicitly specify the size of the input:

    short one, two, three;

    fread( &one, sizeof(short), 1, in );
    fread( &two, sizeof(short), 1, in );
    fread( &three, sizeof(short), 1, in );

Binary files are notoriously nonportable between compilers, not only
because of type sizes.  Especially when reading or writing structs,
different compilers will use different packing/alignment schemes for
struct members.  Even worse, different machine architectures (and
sometimes different operating systems on a given  machine) use different
byte ordering systems for integers.  And don't even think about trying
to read or write floating point numbers!

The FAQ has more information on this subject in chapter 22.9

hth!

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Deductive logic is tautological;    |
|       aka Fighteer I       | there is no way to get a new truth   |
|   mailto:fighteer AT cs DOT com   | out of it."                          |
| http://www.cs.com/fighteer |                 - Lazarus Long       |
---------------------------------------------------------------------

- Raw text -


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