Results 1 to 6 of 6

Thread: Read/write data from file

  1. #1
    Join Date
    Dec 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Read/write data from file

    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:
    Qt Code:
    1. bool Score::readFile() {
    2. QFile file("score.sc");
    3. QDataStream in(&file);
    4. if (!file.open(QIODevice::ReadOnly)) {
    5. ...
    6. return -1;
    7. }
    8.  
    9. quint16 row;
    10. quint16 column;
    11. QString str;
    12.  
    13. while (!in.atEnd()) {
    14. in >> row >> column >> str;
    15. if ( !column%2 ) pl[row].plname = str;
    16. else pl[row].plresult = str.toInt();
    17. }
    18. file.close();
    19. return 1;
    20. }
    21.  
    22. bool Score::writeFile() {
    23. QFile file("score.sc");
    24. if (!file.open(QIODevice::WriteOnly)) {
    25. ...
    26. return -1;
    27. }
    28.  
    29. QDataStream out(&file);
    30.  
    31. for (int row = 0; row < MAX_ROWS; ++row) {
    32. for (int column = 0; column < MAX_COLUMNS; ++column) {
    33. if ( !column%2 ) {
    34. QString str = pl[row].plname;
    35. out << quint16(row) << quint16(column) << str;
    36. } else {
    37. QString str = QString::number(pl[row].plresult);
    38. out << quint16(row) << quint16(column) << str;
    39. }
    40.  
    41. }
    42. }
    43. file.close();
    44. return 1;
    45. }
    46. }
    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:
    Qt Code:
    1. bool Score::readFile() {
    2. QFile file("score.sc");
    3. QDataStream in(&file);
    4. if (!file.open(QIODevice::ReadOnly)) {
    5. ...
    6. return -1;
    7. }
    8.  
    9. quint16 row;
    10. quint16 column;
    11. QString str;
    12.  
    13. int block_num = 2; // = difficulty + 1 (testing version)
    14. file.seek(qint64( (sizeof(quint16)*2 + sizeof(QString)) * 15 * block_num ) );
    15. for (int val_red = 0; val_red < 15; ++val_red) {
    16. in >> row >> column >> str;
    17. if ( !column%2 ) pl[row].plname = str;
    18. else pl[row].plresult = str.toInt();
    19. }
    20. file.close();
    21. return 1;
    22. }
    23.  
    24. bool Score::writeFile() {
    25. QFile file("score.sc");
    26. if (!file.open(QIODevice::WriteOnly)) {
    27. ...
    28. return -1;
    29. }
    30.  
    31. QDataStream out(&file);
    32.  
    33. int block_num = 2;
    34. file.seek(qint64( (sizeof(quint16)*2 + sizeof(QString)) * 15 * block_num ) );
    35. for (int row = 0; row < MAX_ROWS; ++row) {
    36. for (int column = 0; column < MAX_COLUMNS; ++column) {
    37. if ( !column%2 ) {
    38. QString str = pl[row].plname;
    39. out << quint16(row) << quint16(column) << str;
    40. } else {
    41. QString str = QString::number(pl[row].plresult);
    42. out << quint16(row) << quint16(column) << str;
    43. }
    44.  
    45. }
    46. }
    47. file.close();
    48. return 1;
    49. }
    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.
    Last edited by Insomnium; 8th December 2010 at 20:27.

  2. #2
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read/write data from file

    Use QXmlStreamReader and QXmlStreamWriter instead. This is quite easy to use. Writing is very easy. Reading requires a bit more coding, but can be very easy also if you know what data fields to expect.

    Best regards,
    Marc

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Read/write data from file

    Your offset calculation is wrong. The sizeof(QString) is neither the length of the string in bytes nor the size that it would occupy on disk using QDataStream.

    Here are some thoughts:
    • You could read each difficulty block in turn until you found the right one.
    • You could store three separate files.
    • You could note the file offset at the start of each difficulty block and store it at the end of the file as an index during writing. During reading, you load the index from the end of the file and use that to more directly address your difficulty block. This can be done at the front of the file with some care.
    • You could use a text based format like XML and use element IDs to address chunks of the data.

    Incidentally, the nested loops don't seem to be a good fit to a list-of-pairs data structure.

  4. #4
    Join Date
    Dec 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read/write data from file

    Yes, my program worked fine with 3 separated files but I need it to work with one. I decided to use QList to store data. ('difficulty-name-score'). But now I've got one more problem. I can't sort my list by score using qSort() function.
    Qt Code:
    1. qSort(myList->begin(), myList->end(), qGreater<...>());
    To copy to clipboard, switch view to plain text mode 

    How can I handle to pl.score from list in qGreater where pl is a structure 'name-score'?

    Thank you and sorry, I should post it into a newbie questions.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Read/write data from file

    Either provide your pl data structure with an operator<() or provide an external function that provides a similar effect (example in the qSort docs).

  6. #6
    Join Date
    Dec 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read/write data from file

    Thank you, I already found this way. Qt Assistant is a great thing.

Similar Threads

  1. Replies: 2
    Last Post: 2nd November 2010, 05:15
  2. How to read and write data into the QTableView
    By grsandeep85 in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2009, 12:31
  3. file read & write help....
    By aj2903 in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2008, 11:58
  4. structures to a file (read and write)
    By baray98 in forum Qt Programming
    Replies: 5
    Last Post: 10th February 2008, 20:12
  5. Read \Write Excel File using Qt
    By xingshaoyong in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 22:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.