Mail Archives: djgpp/1997/12/31/11:47:31
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 -