char, unsigned char, and binary file io

  • Thread starter Karl Heinz Buchegger
  • Start date
K

Karl Heinz Buchegger

glen_stark said:
Hi.

I have code for reading and writing gds files that I have inherited from
a company and need to make standards compliant. Unfortunately the code
is heavily dependant on the win api. I think I can get around
everything but one stumbling point I would like some advice on.

The win api uses an unsigned char array for writing binary data to and
from the gds file. Now, one reason I want standards compliant code is
to be able to run my code on a variety of different machines, so I can't
count on the same representation of char (as signed or unsigned).

The std iostream however, uses plain char for binary output (please let
me know if I am missing anything). I need to be able to read and write
these files from any machine, and be able to read the files written by
users of the company code.

So what I need to do is make sure that char array I am passing to
std::write contains an unsigned char binary representation.

stick with unsigned char and cast the pointer you give
to read() and write() to char*. read() and write() will do
nothing to the chars if you open the file in binary mode.
 
G

glen_stark

Hi.

I have code for reading and writing gds files that I have inherited from
a company and need to make standards compliant. Unfortunately the code
is heavily dependant on the win api. I think I can get around
everything but one stumbling point I would like some advice on.

The win api uses an unsigned char array for writing binary data to and
from the gds file. Now, one reason I want standards compliant code is
to be able to run my code on a variety of different machines, so I can't
count on the same representation of char (as signed or unsigned).

The std iostream however, uses plain char for binary output (please let
me know if I am missing anything). I need to be able to read and write
these files from any machine, and be able to read the files written by
users of the company code.

So what I need to do is make sure that char array I am passing to
std::write contains an unsigned char binary representation.

How can I take a char, and cast it to a double (or int, or whatever),
interpreting, taking care to interpret it as an unsigned char? And vice
versae?

Thanks for your time.
 

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
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top