Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. reading floats in a struct

reading floats in a struct

Scheduled Pinned Locked Moved C / C++ / MFC
questionannouncement
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jon Hulatt
    wrote on last edited by
    #1

    Hi all, I have a struct a bit like this, which is the header to a file on disk:-

    struct Header
    {
    short a;
    short b;
    short c;
    float floatvalue;
    }

    I read the header from the disk like this:-

    ReadFile(hFile,&header,sizeof(Header),&bytesRead,NULL);

    Once I'd done that, the values in a,b,c are as expected, but the floatvalue is not. However, if I adjust the struct so that instead of float, I have BYTE[4] as follows:-

    struct Header
    {
    short a;
    short b;
    short c;
    BYTE floatvalue[4];
    }

    And then after reading the header as before, I do:-

    float f;
    memcpy(&f,header.floatvalue,sizeof(float));
    

    Then f contains the value expected. I don't understand why the first version didn't work. Can anyone enlighten me please? Thanks Jon

    using System.Beer;

    C 1 Reply Last reply
    0
    • J Jon Hulatt

      Hi all, I have a struct a bit like this, which is the header to a file on disk:-

      struct Header
      {
      short a;
      short b;
      short c;
      float floatvalue;
      }

      I read the header from the disk like this:-

      ReadFile(hFile,&header,sizeof(Header),&bytesRead,NULL);

      Once I'd done that, the values in a,b,c are as expected, but the floatvalue is not. However, if I adjust the struct so that instead of float, I have BYTE[4] as follows:-

      struct Header
      {
      short a;
      short b;
      short c;
      BYTE floatvalue[4];
      }

      And then after reading the header as before, I do:-

      float f;
      memcpy(&f,header.floatvalue,sizeof(float));
      

      Then f contains the value expected. I don't understand why the first version didn't work. Can anyone enlighten me please? Thanks Jon

      using System.Beer;

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      Maybe take a look at this[^], i hope it explains what you need explained. :)

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

      J R 2 Replies Last reply
      0
      • C Code o mat

        Maybe take a look at this[^], i hope it explains what you need explained. :)

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

        J Offline
        J Offline
        Jon Hulatt
        wrote on last edited by
        #3

        Thank for that, it's clear now. Looks like a can get around the issue with #pragma pack (2)

        using System.Beer;

        1 Reply Last reply
        0
        • C Code o mat

          Maybe take a look at this[^], i hope it explains what you need explained. :)

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

          R Offline
          R Offline
          RomTibi
          wrote on last edited by
          #4

          I think that a

          #pragma pack (1)

          or

          #pragma pack (2)

          declaration before the structure's definition would help. Think so?

          36. When you surround an army, leave an outlet free. ... Do not press a desperate foe too hard. SUN-TZU - Art of War

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups