Standardizing size of float across multiple systems

Hey all,

I've got an interesting question for you that's more about application deployment than anything else. I'm currently writing a cross-platform on screen keyboard application that highlights keys on the screen as you type them. My approach was to use an image of a keyboard and layer semi-transparent sprites over each key on key events, and as a consequence I needed to map out precise positions of each of these sprites relative to the background image.

The issue became how to load those positions into my application during runtime? My solution was to use excel to make a .csv of all the x-y coordinates, but instead of just deploying the .csv with my application I decided to write a quick app that parsed each line of the csv for coordinate pairs and write those floating point numbers to a binary file instead. That way the end user can't easily edit the coordinates of the sprites.

Now on to my question. The way I load the data into my application is by this method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <fstream>
#include <vector>
/* other includes */

/* ... runtime code ... */

std::vector<float> pixmapPosData;

bool loadPixmapPosData()
{
    float number;
    int j = 0;
    streampos length;
    ifstream file;

    file.open("keyboardPosData.bin", ios::in | ios::binary);
    if(!file.is_open())
        return false;

    file.seekg(0,ios::end);
    length = file.tellg();
    file.seekg(0,ios::beg);

    while(j < length)
    {
        j += sizeof(number);
        file.read(reinterpret_cast<char *>(&number),sizeof(number));
        pixmapPosData.push_back(number);
    }

    file.close();
    return true;
}


This works fine on my system, but what if sizeof(float) has a different length their system? Then this function could throw a read access violation towards the end of the file (since the .bin file is exactly 508*sizeof(float) bytes long on my computer), and not to mention the coordinates would be meaningless. Is there any way to avoid this? Or maybe I just just suck it up and deploy a .csv file?

Best Regards,

- Weikarczaena
Last edited on
I recently wrote a very good file serialization/deserialization class based on templates and found myself with the same problem, should I store it as a float or as a string?

The most portable way is actually a stringized float.
If you want to try harder, you can approximate a float by using two integers.
Or you can simply put out a restriction for your program for systems who have 4byte IEEE-754 floats only.
The most portable way is actually a stringized float.

Okay, but then how do I stop the user from editing these files? Especially since .csv is an associated file type with Microsoft Excel, so people might see this and mess with it causing my application to fail.
Last edited on
A simple XOR-based encryption should suffice. Otherwise, display an error if the floats aren't valid.
Topic archived. No new replies allowed.