Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to QFile::map a binary file filled with double values
Qt 6.11 is out! See what's new in the release blog

How to QFile::map a binary file filled with double values

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.9k 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.
  • R Offline
    R Offline
    RTbecard
    wrote on last edited by
    #1

    I'm having trouble understanding how QFile::map works. The return value is a uchar*, which I understand as an array of characters. In the documentation I don't see any arguments for specifying the type of file to map to (int32, double, float, etc).

    In my case, I created a binary file filled with double values and now I want to map that to memory. Can someone provide a very brief example on how this is achieved?

    S jsulmJ 2 Replies Last reply
    0
    • R RTbecard

      I'm having trouble understanding how QFile::map works. The return value is a uchar*, which I understand as an array of characters. In the documentation I don't see any arguments for specifying the type of file to map to (int32, double, float, etc).

      In my case, I created a binary file filled with double values and now I want to map that to memory. Can someone provide a very brief example on how this is achieved?

      S Offline
      S Offline
      Stoyan
      wrote on last edited by
      #2

      @RTbecard
      I think that QFile::map create identical copy of content of a file or part of it into the memory - byte by byte. Every uchar is a byte from memory. So, it is up to you how to interpret the content of this part of the memory. If you map your whole file and it contain only doubles, I guess you can cast it like this:

      uchar * map = file.map(0, size);
      double * dm = map;
      double dbl1 = *dm[0];
      double dbl2 = *dm[1];
      ...
      

      Well, I'm not completely sure about this.

      1 Reply Last reply
      0
      • R RTbecard

        I'm having trouble understanding how QFile::map works. The return value is a uchar*, which I understand as an array of characters. In the documentation I don't see any arguments for specifying the type of file to map to (int32, double, float, etc).

        In my case, I created a binary file filled with double values and now I want to map that to memory. Can someone provide a very brief example on how this is achieved?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @RTbecard @Stoyan
        Just use reinterpret_cast:

        uchar * map = file.map(0, size);
        double * dm = reinterpret_cast<double*>(map);
        double dbl1 = *dm[0];
        double dbl2 = *dm[1];
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved