portability problem

C

copx

I want to read/write a specific binary data file format. The issue is
that the format uses 4 byte integer values and how do you read/write
those on a platform that has no native 4 byte integer data type?
Actually all platforms currently supported by my program have such a
type but I want to make the program more portable. My goal is to support
all platforms that have a native integer data type at least 4 byte wide.
Can I just use int_least32_t like this (remember I am trying to
read/write a 4 byte integer here):

reading:
...
int_least32_t foo = 0;
...
fread(&foo, 4, 1, in_file);
...


writing:
....
fwrite(&foo, 4, 1, out_file);
...
 
V

Vladimir Oka

copx said:
I want to read/write a specific binary data file format. The issue is
that the format uses 4 byte integer values and how do you read/write
those on a platform that has no native 4 byte integer data type?
Actually all platforms currently supported by my program have such a
type but I want to make the program more portable. My goal is to support
all platforms that have a native integer data type at least 4 byte wide.
Can I just use int_least32_t like this (remember I am trying to
read/write a 4 byte integer here):

reading:
..
int_least32_t foo = 0;
..
fread(&foo, 4, 1, in_file);
..


writing:
...
fwrite(&foo, 4, 1, out_file);
..

You always have an option of reading the data byte by byte, and storing
it in an array of byte sized elements (e.g. signed or unsigned char).
You can then convert this array to a appropriately sized value,
internally. Writing back to a file then just needs converting the value
to an array again, and writing elements of that array. It may even help
you with possible endianness problems.
 
F

Flash Gordon

Yes, you can use int_least32_t or int_fast32_t depending on whether you
prefer speed or being as close as possible to 32 bits.

No, not like this.

Nor this.
You always have an option of reading the data byte by byte, and storing
it in an array of byte sized elements (e.g. signed or unsigned char).
You can then convert this array to a appropriately sized value,
internally. Writing back to a file then just needs converting the value
to an array again, and writing elements of that array. It may even help
you with possible endianness problems.

Yes, Vladimir is correct that the best way is to convert the data
to/from the appropriate binary format manually. Endianness is a real
problem for some of us since the IBM servers running AIX and the x86
servers have opposite endianness.
 
R

Richard Bos

copx said:
I want to read/write a specific binary data file format. The issue is
that the format uses 4 byte integer values and how do you read/write
those on a platform that has no native 4 byte integer data type?
Actually all platforms currently supported by my program have such a
type but I want to make the program more portable. My goal is to support
all platforms that have a native integer data type at least 4 byte wide.
Can I just use int_least32_t like this (remember I am trying to
read/write a 4 byte integer here):

No, because for starters you still have the endianness problem. If the
format just specifies "4 byte integer", ask for clarification. If the
format specifies "4 byte large/small-endian integer", read in the four
octets, and shift them into the integer in the required order; and v.v.,
shift out/mask the four octets and write them to the file in that order.

Richard
 
C

copx

copx said:
I want to read/write a specific binary data file format. The issue is
that the format uses 4 byte integer values and how do you read/write
those on a platform that has no native 4 byte integer data type?
[snip]

Thanks everyone!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top