Results 1 to 7 of 7

Thread: Index out of range error for a txt input file

  1. #1
    Join Date
    Mar 2011
    Location
    Johannesburg, South Africa
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Index out of range error for a txt input file

    I had this section of code wording a few days ago, now having fixed other things it has stopped! The screen has two inverted commas " and no output.

    The output file lines look like this.

    Qt Code:
    1. 1, Lata Becker, 29 Atterbury Road, 1110, Port Elizabeth, 074 3546902
    2. 2,CD Viljoen, 1 Kelvin, 0010, Victoria, 022 4699087
    To copy to clipboard, switch view to plain text mode 

    I create the file from

    Qt Code:
    1. QString Contact::toString()const {
    2. QString result = QString::number(category,10) + ", " + firstName + " " +
    3. lastName + ", " + streetAddress + ", " + zipCode + ", " + city + ", " +
    4. phoneNumber;
    5. return result;
    6. }
    To copy to clipboard, switch view to plain text mode 

    The \n is added by the calling function. There are no start or end characters in the file.
    The writer appears to write correctly - this is the reader that crashes...

    Qt Code:
    1. void ContactListReader::read(ContactList &mylist)
    2. {
    3. QFile infile(fn);
    4. infile.open(QIODevice::ReadOnly);
    5. QTextStream out(&infile);
    6. while(!infile.atEnd()) {
    7. QString line = infile.readLine();
    8. if(line != "\n") {
    9. QStringList list = line.split(", ");
    10. QString fn = (list.at(1).split(" ")).at(0);
    11. QString ln = (list.at(1).split(" ")).at(1);
    12. mylist.add(Contact((list.at(0)).toInt(),fn,ln,list.at(2),list.at(3),list.at(4),list.at(5)));
    13. }
    14. }
    15. infile.close();
    16. }
    To copy to clipboard, switch view to plain text mode 

    Should I be looking beyond these items for a solution? An included file etc?
    Last edited by april26; 8th April 2011 at 19:44. Reason: wrong output used

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Index out of range error for a txt input file

    "The writer appears to write correctly"

    Appears to? It's writing a text file. Can you open the file in a text editor? Do the contents look correct?

  3. #3
    Join Date
    Mar 2011
    Location
    Johannesburg, South Africa
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Index out of range error for a txt input file

    Yes, it creates the file and looks fine. The two lines I included are from the file, in case someone could see something in the formatting that was wrong.

    Sorry the headline of the thread is incorrect - I no longer get the out of range error - now it is just that there is no output. If the reader looks correct, it could be that the data is being read in but I have made a mistake with the output. When I run the output on it's own (using data that doesn't come from a text file but from main) output does work correctly.

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Index out of range error for a txt input file

    What happens when you print out the extracted strings (fn, ln) as you process the file?

  5. #5
    Join Date
    Mar 2011
    Location
    Johannesburg, South Africa
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Index out of range error for a txt input file

    Thank you for your help. When I placed a qDebug << statement for the ln and ln variables, the firstname and lastname are output correctly. Do you think they are not being "added" properly to QList mylist?


    Added after 4 minutes:


    #include "contactlistreader.h"
    #include <QFile>
    #include <QTextStream>
    #include <QStringList>
    #include <QString>
    #include <QDebug>

    These are my include file in case they could be the problem. This has 5 .h class files - I'm going to try including them all just in case...
    Last edited by april26; 8th April 2011 at 20:03.

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Index out of range error for a txt input file

    You are hacking your way into problems:
    1) you declare QTextStream out - and never use it...
    2) the QTextStream is named out because you don't use it to read the file
    3) you don't check that the string fn (i assume that is a QString) is actually a file path/name that exist...

    I don't see the problem, but my advice is: organize your code, don't just test in your actual project and then you don't know what's for testing and what's actual project, if you need a quick test to see how to write some small functionality - write it in a small separated project and test there.

    And you use ContactList - is a hand-written container? or what is that? are you sure the bug is not there - use the debugger and you will see (or as SixDegrees said print to console as you read - and don't forget to check the file name)

  7. #7
    Join Date
    Mar 2011
    Location
    Johannesburg, South Africa
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Index out of range error for a txt input file

    I wrote each section separately and they were working. Now that I'm putting them all together, this function isn't.

    I am a terrible programmer I've discovered. I'm in my final year of a BSc, and programming isn't my major but this course has to be passed. I've struggled with each and every programming course but in the end I did well - I am doing distance learning so trial and error is pretty much the only option I have. The text books are crazy (we are using EZust) - they present one-line code in "main" which is easy to understand, but the assignments are about subjects not included in Ezust. I think the assumption is that you work for a software company with lots of programmers and that you work with C++ every day - who would be crazy enough to do a BSc by correspondence!

    Contact and Contactlist are both handwritten containers. There are 5 .h and .cpp files in all - I can post them here if it would help?


    Added after 8 minutes:


    I've just realised I've used the same variable twice. fn is filename and fn is firstname. However correcting this did not change anything.

    Does QT have a cache? I went off for a cup of coffee (I closed the project), when I opened, it works?

    I will not question, I will just be grateful :-)
    Last edited by april26; 8th April 2011 at 21:14.

Similar Threads

  1. Replies: 7
    Last Post: 18th September 2009, 14:44
  2. Replies: 1
    Last Post: 27th March 2009, 06:22
  3. Index out of Range
    By santhoshv84 in forum Qt Programming
    Replies: 2
    Last Post: 19th August 2008, 15:33
  4. QList index out of range problem
    By MarkoSan in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2008, 08:40
  5. Qt 4.1.4 on VS2005 error- cannot open input file 'qtmain.lib'
    By Ashish in forum Installation and Deployment
    Replies: 10
    Last Post: 11th October 2006, 16:05

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.