Re: Reading little-endian data from a file in a portable manner
- From: Thad Smith <ThadSmith@xxxxxxx>
- Date: Mon, 19 Jul 2010 19:28:25 -0700
Malcolm McLean wrote:
On Jul 16, 6:07 pm, Ben Bacarisse <ben.use...@xxxxxxxxx> wrote:Malcolm McLean <malcolm.mcle...@xxxxxxxxxxxxxx> writes:Except it doesn't, because on that architecture the routine will break
int x;This, too can be UB if fgetc(fp) times 256 is not representable as an
x = fgetc(fp);
x |= (fgetc(fp) << 8);
int (and this is the usual case on a 16-bit machine). Again, if you use
unsigned types the problem goes away.
when you convert the unsigned to a signed.
The only way I can see of doing it is to manually apply 2s complement
conversion on an unsigned. Which isn't really acceptable.
In the grand scheme of portable data transfer, one must specify the encoding of data in the file. Can there be negative values? If so, how are they encoded? There are more than way. Pick one, then ensure that code that writes encodes properly and code that reads decodes properly.
--
Thad
.
- Follow-Ups:
- Re: Reading little-endian data from a file in a portable manner
- From: Malcolm McLean
- Re: Reading little-endian data from a file in a portable manner
- References:
- Reading little-endian data from a file in a portable manner
- From: chrisbazley
- Re: Reading little-endian data from a file in a portable manner
- From: Malcolm McLean
- Re: Reading little-endian data from a file in a portable manner
- From: Ben Bacarisse
- Re: Reading little-endian data from a file in a portable manner
- From: Malcolm McLean
- Reading little-endian data from a file in a portable manner
- Prev by Date: Re: malloc and alignment question
- Next by Date: Re: Code organization / initialize global array
- Previous by thread: Re: Reading little-endian data from a file in a portable manner
- Next by thread: Re: Reading little-endian data from a file in a portable manner
- Index(es):
Relevant Pages
|