Important: Please read the Qt Code of Conduct -

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

  • 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?

  • @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 =, size);
    double * dm = map;
    double dbl1 = *dm[0];
    double dbl2 = *dm[1];

    Well, I'm not completely sure about this.

  • Lifetime Qt Champion

    @RTbecard @Stoyan
    Just use reinterpret_cast:

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

Log in to reply