char, unsigned char, and binary file io

Discussion in 'C++' started by Karl Heinz Buchegger, Apr 22, 2004.

  1. glen_stark wrote:
    >
    > 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.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Apr 22, 2004
    #1
    1. Advertising

  2. Karl Heinz Buchegger

    glen_stark Guest

    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.
     
    glen_stark, Apr 22, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Junmin H.
    Replies:
    20
    Views:
    1,015
    Charlie Gordon
    Sep 20, 2007
  2. Ioannis Vranos
    Replies:
    11
    Views:
    762
    Ioannis Vranos
    Mar 28, 2008
  3. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    614
    Ben Bacarisse
    Mar 29, 2008
  4. Alex Vinokur
    Replies:
    9
    Views:
    798
    James Kanze
    Oct 13, 2008
  5. pozz
    Replies:
    12
    Views:
    743
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page