First of all execuse me for my English please. I'm writing a game. High scores will be stored in file reading and writing with QDataStream. Then scores will be shown in table view. There are 3 difficulty levels in my game and all the information will be stored in one file. Everything worked fine for one diff. level, but I've got troubles organizing work with 3 levels. Here is my code:
bool Score::readFile() {
...
return -1;
}
quint16 row;
quint16 column;
while (!in.atEnd()) {
in >> row >> column >> str;
if ( !column%2 ) pl[row].plname = str;
else pl[row].plresult = str.toInt();
}
file.close();
return 1;
}
bool Score::writeFile() {
...
return -1;
}
for (int row = 0; row < MAX_ROWS; ++row) {
for (int column = 0; column < MAX_COLUMNS; ++column) {
if ( !column%2 ) {
out << quint16(row) << quint16(column) << str;
} else {
out << quint16(row) << quint16(column) << str;
}
}
}
file.close();
return 1;
}
}
bool Score::readFile() {
QFile file("score.sc");
QDataStream in(&file);
if (!file.open(QIODevice::ReadOnly)) {
...
return -1;
}
quint16 row;
quint16 column;
QString str;
while (!in.atEnd()) {
in >> row >> column >> str;
if ( !column%2 ) pl[row].plname = str;
else pl[row].plresult = str.toInt();
}
file.close();
return 1;
}
bool Score::writeFile() {
QFile file("score.sc");
if (!file.open(QIODevice::WriteOnly)) {
...
return -1;
}
QDataStream out(&file);
for (int row = 0; row < MAX_ROWS; ++row) {
for (int column = 0; column < MAX_COLUMNS; ++column) {
if ( !column%2 ) {
QString str = pl[row].plname;
out << quint16(row) << quint16(column) << str;
} else {
QString str = QString::number(pl[row].plresult);
out << quint16(row) << quint16(column) << str;
}
}
}
file.close();
return 1;
}
}
To copy to clipboard, switch view to plain text mode
And here is what I'm trying to do next for working with 3 difficulty levels:
bool Score::readFile() {
...
return -1;
}
quint16 row;
quint16 column;
int block_num = 2; // = difficulty + 1 (testing version)
file.
seek(qint64
( (sizeof(quint16
)*2 + sizeof(QString)) * 15 * block_num
) );
for (int val_red = 0; val_red < 15; ++val_red) {
in >> row >> column >> str;
if ( !column%2 ) pl[row].plname = str;
else pl[row].plresult = str.toInt();
}
file.close();
return 1;
}
bool Score::writeFile() {
...
return -1;
}
int block_num = 2;
file.
seek(qint64
( (sizeof(quint16
)*2 + sizeof(QString)) * 15 * block_num
) );
for (int row = 0; row < MAX_ROWS; ++row) {
for (int column = 0; column < MAX_COLUMNS; ++column) {
if ( !column%2 ) {
out << quint16(row) << quint16(column) << str;
} else {
out << quint16(row) << quint16(column) << str;
}
}
}
file.close();
return 1;
}
bool Score::readFile() {
QFile file("score.sc");
QDataStream in(&file);
if (!file.open(QIODevice::ReadOnly)) {
...
return -1;
}
quint16 row;
quint16 column;
QString str;
int block_num = 2; // = difficulty + 1 (testing version)
file.seek(qint64( (sizeof(quint16)*2 + sizeof(QString)) * 15 * block_num ) );
for (int val_red = 0; val_red < 15; ++val_red) {
in >> row >> column >> str;
if ( !column%2 ) pl[row].plname = str;
else pl[row].plresult = str.toInt();
}
file.close();
return 1;
}
bool Score::writeFile() {
QFile file("score.sc");
if (!file.open(QIODevice::WriteOnly)) {
...
return -1;
}
QDataStream out(&file);
int block_num = 2;
file.seek(qint64( (sizeof(quint16)*2 + sizeof(QString)) * 15 * block_num ) );
for (int row = 0; row < MAX_ROWS; ++row) {
for (int column = 0; column < MAX_COLUMNS; ++column) {
if ( !column%2 ) {
QString str = pl[row].plname;
out << quint16(row) << quint16(column) << str;
} else {
QString str = QString::number(pl[row].plresult);
out << quint16(row) << quint16(column) << str;
}
}
}
file.close();
return 1;
}
To copy to clipboard, switch view to plain text mode
Let me explain. There are 15 pairs 'name - result' stored in a structure pl for every difficulty level. block_num equals 2 for a while, but later I'll transfer them as function parameters. And at fact file can be divided into 3 parts. I believe my problem is in calculating size of "blocks". But there is something wrong with reading (or even writing) data because I've got unexpected output after read/write operations. Help me please. Thank you.
Bookmarks