vendredi 27 novembre 2020

File readable but saved in binary mode

I wrote a very simple function that saves a file in binary mode with the support of qt. The file is saved correctly and the data inside is correct, however if I open the file with a text editor I can read strings that I shouldn't be reading.

void Game::saveRanking() const
{
    QFile file(".ranking.dat");
    file.open(QFile::WriteOnly);

    QJsonObject recordObject;

    QJsonArray rankingNameArray;
    for (int i = 0; i < 5; i++)
        rankingNameArray.push_back(QJsonValue::fromVariant(QVariant(highscoreName[i])));
    recordObject.insert("Ranking Name", rankingNameArray);

    QJsonArray rankingScoreArray;
    for (int i = 0; i < 5; i++)
        rankingScoreArray.push_back(QJsonValue::fromVariant(QVariant(highscoreValue[i])));
    recordObject.insert("Ranking Value", rankingScoreArray);

    QJsonDocument doc(recordObject);
    file.write(doc.toBinaryData());
}

I've filled the arrays like this, for debugging purposes

highscoreName[0] = "Pippo"; highscoreValue[0] = 100;
highscoreName[1] = "Franco"; highscoreValue[1] = 300;
highscoreName[2] = "Giovanni"; highscoreValue[2] = 200;
highscoreName[3] = "Andrea"; highscoreValue[3] = 4000;
highscoreName[4] = "AI"; highscoreValue[4] = 132400;

I tried to do a hexdump and the result is the following

0000-0010:  71 62 6a 73-01 00 00 00-a4 00 00 00-05 00 00 00  qbjs.... ........
0000-0020:  9c 00 00 00-14 04 00 00-0c 00 52 61-6e 6b 69 6e  ........ ..Rankin
0000-0030:  67 20 4e 61-6d 65 00 00-48 00 00 00-0a 00 00 00  g.Name.. H.......
0000-0040:  34 00 00 00-05 00 50 69-70 70 6f 00-06 00 46 72  4.....Pi ppo...Fr
0000-0050:  61 6e 63 6f-08 00 47 69-6f 76 61 6e-6e 69 00 00  anco..Gi ovanni..
0000-0060:  06 00 41 6e-64 72 65 61-02 00 41 49-8b 01 00 00  ..Andrea ..AI....
0000-0070:  8b 02 00 00-8b 03 00 00-0b 05 00 00-0b 06 00 00  ........ ........
0000-0080:  94 0f 00 00-0d 00 52 61-6e 6b 69 6e-67 20 56 61  ......Ra nking.Va
0000-0090:  6c 75 65 00-20 00 00 00-0a 00 00 00-0c 00 00 00  lue..... ........
0000-00a0:  8a 0c 00 00-8a 25 00 00-0a 19 00 00-0a f4 01 00  .....%.. ........
0000-00ac:  0a a6 40 00-0c 00 00 00-68 00 00 00              ..@..... h...

Aucun commentaire:

Enregistrer un commentaire