Results 1 to 6 of 6

Thread: Read/write data from file

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.