don't click here

Reading binary values C++

Discussion in 'Technical Discussion' started by Alriightyman, Oct 3, 2014.

Thread Status:
Not open for further replies.
  1. Alriightyman

    Alriightyman

    I am back... from the dead! Tech Member
    357
    11
    18
    Somewhere in hot, death Florida
    0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
    Writing float data to a binary file and then reading it.

    http://pastebin.com/EZxWqKH8
    And here is the output:
    Code (Text):
    1.  
    2. 1.01234        1.01234
    3. 2.01234        0.012343
    4. 3.01234        0.012344
    5. 4.01234        0.012345
    6. 5.01234        0.01234
    7.  
    EDIT: Serious Fromating issues
     
  2. GerbilSoft

    GerbilSoft

    RickRotate'd. Administrator
    2,971
    76
    28
    USA
    rom-properties
    The output file is:

    Code (Text):
    1.  
    2. 00000000  31 2e 30 31 32 33 34 32  2e 30 31 32 33 34 33 2e  |1.012342.012343.|
    3. 00000010  30 31 32 33 34 34 2e 30  31 32 33 34 35 2e 30 31  |012344.012345.01|
    4. 00000020  32 33 34                                          |234|
    5.  
    You're printing the string representations of the floating-point numbers without separators. cin has no idea how to separate them, so it gets the values wrong.

    You need to add spaces between the numbers, either an actual space character or a newline.

    If you wanted to print the binary representation, you'll need to print the raw bytes, which is more easily done using C stdio than with iostream. (Note that this may cause endian issues if you switch between little-endian and big-endian systems.)
     
  3. Alriightyman

    Alriightyman

    I am back... from the dead! Tech Member
    357
    11
    18
    Somewhere in hot, death Florida
    0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
    Thanks, but why is it writing the string representation of the float value? I thought it should write it as a 32-bit binary.
     
  4. flamewing

    flamewing

    Emerald Hunter Tech Member
    1,161
    65
    28
    France
    Sonic Classic Heroes; Sonic 2 Special Stage Editor; Sonic 3&K Heroes (on hold)
    The >> and << operators are for formatted input and output; it converts the values passed into strings based on one of a dozen overloads.

    What you want is to either use the read or write member functions of the streams; e.g.:
    Code (Text):
    1.     file.write(reinterpret_cast<char const *>(&outArray[I]), sizeof(outArray[I]));
    Edit: But as GerbilSoft mentions, this can lead to big- versus little-endian issues.

    Edit 2: Additional reading: output, input.
     
  5. Alriightyman

    Alriightyman

    I am back... from the dead! Tech Member
    357
    11
    18
    Somewhere in hot, death Florida
    0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
    Thanks a lot! That helped.

    I'm not worried about big-/little-endian issues for this little project, but I will keep it in mind.
     
Thread Status:
Not open for further replies.