Results 1 to 7 of 7

Thread: QFile reading problem

  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Question QFile reading problem

    write:
    Qt Code:
    1. QFile file(QDir::homePath() + "/datedata.txt");
    2. if (file.open(QIODevice::WriteOnly |QIODevice::Text | QIODevice::Append)) {
    3. QTextStream out(&file);
    4. out << timeCount << endl;
    5. out << dateEdit->date().toString("MMMM dd, yyyy") << endl;
    6. for(int j = 0; j<timeCount; j++){
    7. out << timeEditList[j]->text() << endl;
    8. out << textEditList[j]->toPlainText() << endl;
    9. for(int k = 0; k<7; k++){
    10. out << numbers[j][k] << endl;
    11. }
    12. }
    13.  
    14. }
    15. file.close();
    To copy to clipboard, switch view to plain text mode 

    read:
    Qt Code:
    1. QFile file(QDir::homePath() + "/datedata.txt");
    2. if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
    3. QTextStream in(&file);
    4. while(!in.atEnd()){
    5. numberOfTimes << in.readLine().toInt();
    6. dates << in.readLine();
    7. for(int j = 0; j<numberOfTimes.size(); j++){
    8. times << in.readLine();
    9. foods << in.readLine();
    10. for(int k = 0; k<7; k++){
    11. totalValues << in.readLine();
    12. }
    13. }
    14. }
    15. }
    16. file.close();
    To copy to clipboard, switch view to plain text mode 

    I used these couts to write what its reading
    Qt Code:
    1. for(int i = 0; i<numberOfTimes.size(); i++){
    2. cout << "number of times " << i << ": " << numberOfTimes[i] << endl;
    3. }
    4. for(int i = 0; i<dates.size(); i++){
    5. cout << "date " << i << ": " << dates[i].toStdString() << endl;
    6. }
    7. for(int i = 0; i<times.size(); i++){
    8. cout << "time " << i << ": " << times[i].toStdString() << endl;
    9. cout << "foods " << i << ": " << foods[i].toStdString() << endl;
    10. for(int j = 0; j<7; j++){
    11. cout << "total values " << j << ": " << totalValues[j+(7*i)].toStdString() << endl;
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    this part works good if I have this for example: date 1, 1 time, with 1->infinte foods, or date 2, 1->infinte times, with ONLY 1 food per line
    example: date 1, times 1, foods "Banana, Banana" date 2, times 2, food 1 "Apple" food 2 "Apple"
    number of times 0: 1
    number of times 1: 2
    date 0: April 04, 2013
    date 1: April 04, 2013
    time 0: 2:46:11 PM
    foods 0: Banana, Banana
    total values 0: 2
    total values 1: 2
    total values 2: 2
    total values 3: 2
    total values 4: 2
    total values 5: 2
    total values 6: 2
    time 1: 2:46:18 PM
    foods 1: Apple
    total values 0: 0
    total values 1: 0
    total values 2: 0
    total values 3: 0
    total values 4: 0
    total values 5: 0
    total values 6: 0
    time 2: 2:46:20 PM
    foods 2: Apple
    total values 0: 0
    total values 1: 0
    total values 2: 0
    total values 3: 0
    total values 4: 0
    total values 5: 0
    total values 6: 0
    The problem though I am having is if say I have one ore more dates and two times and for time 1 I have the foods "Apple, Orange" and for time 2 I have "Candy" I will get this as an output:
    number of times 0: 2
    number of times 1: 0
    date 0: April 04, 2013
    date 1: Candy
    time 0: 2:54:33 PM
    foods 0: Apple, Orange
    total values 0: 1
    total values 1: 1
    total values 2: 1
    total values 3: 1
    total values 4: 1
    total values 5: 1
    total values 6: 1
    time 1: 1.27
    foods 1: 12.5
    total values 0: 8
    total values 1: 9.04
    total values 2: 21
    total values 3: 3
    total values 4: 81
    total values 5:
    total values 6:
    time 2:
    foods 2:
    total values 0:
    total values 1:
    total values 2:
    total values 3:
    total values 4:
    total values 5:
    total values 6:
    as you can see there is clearly a problem with its reading here is what the text file looks like after writing to it:
    Qt Code:
    1. 2
    2. April 04, 2013
    3. 2:54:33 PM
    4. Apple, Orange
    5. 1
    6. 1
    7. 1
    8. 1
    9. 1
    10. 1
    11. 1
    12. 2:54:43 PM
    13. Candy
    14. 1.27
    15. 12.5
    16. 8
    17. 9.04
    18. 21
    19. 3
    20. 81
    To copy to clipboard, switch view to plain text mode 

    so it should read in this:
    number of times 0: 2
    date 0: April 04, 2013
    time 0: 2:54:33 PM
    foods 0: Apple, Orange
    total values 0: 1
    total values 1: 1
    total values 2: 1
    total values 3: 1
    total values 4: 1
    total values 5: 1
    total values 6: 1
    time 1: 2:54:43 PM
    foods 1: Candy
    total values 0: 1.27
    total values 1: 12.5
    total values 2: 8
    total values 3: 9.04
    total values 4: 21
    total values 5: 3
    total values 6: 81
    It seems like it is reading the file twice for some reason when I have multiple times and one of the times has more than 1 food I can have multiple times with 1 food on all of them and it will work fine. or I can have 1 time and multiple foods on that line. I need to make it so I can read in multiple foods on multiple times not one or the other.

    Thanks for any suggestions/help I appreciate it.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFile reading problem

    use your debugger. Also read my sig.
    Last edited by amleto; 4th April 2013 at 23:39.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: QFile reading problem

    to be honest I don't know how to use debugger..but I just found where the error was now I need to think of a way to fix it there error is this line on the read
    Qt Code:
    1. for(int i = 0; i<numberOfTimes.size(); i++)
    To copy to clipboard, switch view to plain text mode 

    thats why it is reading it weird because its supposed to be like numberOfTimes[i] and i would be the numberOfTimesSize() since numberoftimes size in my example would be 1 but number of times is 2...

    EDIT::Argh sorry to waste a forum spot this code works idk why I didn't spot that error earlier
    Qt Code:
    1. int readSize = 0;
    2. QFile file(QDir::homePath() + "/datedata.txt");
    3. if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
    4. QTextStream in(&file);
    5. while(!in.atEnd()){
    6. numberOfTimes << in.readLine().toInt();
    7. dates << in.readLine();
    8. for(int i = 0; i<numberOfTimes[readSize]; i++){
    9. times << in.readLine();
    10. foods << in.readLine();
    11. for(int j = 0; j<7; j++){
    12. totalValues << in.readLine();
    13. }
    14. }
    15. readSize++;
    16. }
    17. }
    18. file.close();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFile reading problem

    Quote Originally Posted by giblit View Post
    to be honest I don't know how to use debugger..but I just found where the error was now I need to think of a way to fix it there error is this line on the read
    learning how to use your debugger is not optional. you cannot expect to have every issue resolved on a forum where the solution is to use a debugger.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: QFile reading problem

    well when I click the debug button instead of the run button it runs the same way except it has a menu that appears on the button that says stack/qml inspector but it never shows any text or anything I mainly use cout't to see where my problems are

  6. #6
    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: QFile reading problem

    If you are using Qt Creator then find a line of source code, put your text cursor in it, press F9, notice the red marker that appears against the line: this called a breakpoint. Run the debug version of the program with theRun Debug tool button (or press F5). The program pauses when it reaches your line. Notice that the stack, local variables and other debugger panels contain stuff now. Explore these panels, and the options on the Debug menu or debugger toolbar.

  7. The following user says thank you to ChrisW67 for this useful post:

    giblit (5th April 2013)

  8. #7
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: QFile reading problem

    so that's what that red marker is for lol thanks, you can also click on the left margin and it appears because I noticed it before but never knew what it was. And wow all those variables and stuff...alot easier than couting everything lol no wonder it takes me like an hour or two to figure out I am missing something some times

Similar Threads

  1. Reading file using QFile class
    By mania in forum Qt Programming
    Replies: 2
    Last Post: 2nd April 2013, 16:12
  2. reading hex data from QFile
    By Charvi in forum Qt Programming
    Replies: 2
    Last Post: 30th October 2012, 11:54
  3. Reading from QFile when QTextStream is open
    By dawwin in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2011, 17:43
  4. Problems reading content of QFile
    By martinn in forum Newbie
    Replies: 12
    Last Post: 6th April 2010, 18:42
  5. Reading File using QFile and QTextStream
    By atm in forum Qt Programming
    Replies: 4
    Last Post: 13th July 2006, 23:12

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.