Reading binary float from file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #1

    Reading binary float from file

    Hi,
    I'm trying to write code in C++ that loads .3ds files. .3ds files use floats in binary for vertex coordinates, but I don't know how to read them. I googled stuff for a bit, until it seemed like reinterpret_cas t was the way to do it, but this code
    Code:
    float readFloat(ifstream& file) {
        char bytes[sizeof(float)];
        for (int i = 0; i < sizeof(float); i++)
            bytes[i] = file.get();
        return reinterpret_cast<float>(bytes);
    }
    gives me this error:
    invalid reinterpret_cas t from type `char*' to type `float'
    Do any of you know how to read a float from a file? I will appreciate any help you can give me.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    After getting the data try to use atof to get the float value.

    Raghu

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Make that last line:

      [code=c++]
      return *reinterpret_ca st<float*>(byte s);
      [/code]

      kind regards,

      Jos

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Errr...
        Why can't you just:

        [code=cpp]
        float readFloat(ifstr eam& file) {
        float bytes;
        file >> bytes;
        return bytes;
        }
        [/code]

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          Originally posted by weaknessforcats
          Errr...
          Why can't you just:
          Because it assumes it's ascii string like 1.23e4, not 4 bytes of ieee754

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            Which "it" do you mean? The file format or the C++? The C++ most certainly assumes it's 4 bytes of IEEE754.

            Comment

            • boxfish
              Recognized Expert Contributor
              • Mar 2008
              • 469

              #7
              Hi,
              Thanks for all your replies. This works. Yay.
              Code:
              float readFloat(ifstream& file) {
                  char bytes[sizeof(float)];
                  for (int i = 0; i < sizeof(float); i++)
                      bytes[i] = file.get();
                  float* floatPtr = reinterpret_cast<float*>(bytes);
                  return *floatPtr;
              }
              Well, it compiles anyway. Should load .3ds files when I get my program doing that. Thanks Jos. And hopefully when I post this message I will be upgraded to a member. Hooray.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by boxfish
                Hi,
                Thanks for all your replies. This works. Yay.
                Code:
                float readFloat(ifstream& file) {
                    char bytes[sizeof(float)];
                    for (int i = 0; i < sizeof(float); i++)
                        bytes[i] = file.get();
                    float* floatPtr = reinterpret_cast<float*>(bytes);
                    return *floatPtr;
                }
                Well, it compiles anyway. Should load .3ds files when I get my program doing that. Thanks Jos. And hopefully when I post this message I will be upgraded to a member. Hooray.
                Also have a look at the istream::read function to get rid of that silly loop ;-)

                kind regards,

                Jos

                Comment

                • boxfish
                  Recognized Expert Contributor
                  • Mar 2008
                  • 469

                  #9
                  Alright, the loop's gone.

                  Comment

                  Working...