PDA

View Full Version : Index out of range error for a txt input file



april26
8th April 2011, 17:52
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.


1, Lata Becker, 29 Atterbury Road, 1110, Port Elizabeth, 074 3546902
2,CD Viljoen, 1 Kelvin, 0010, Victoria, 022 4699087

I create the file from


QString Contact::toString()const {
QString result = QString::number(category,10) + ", " + firstName + " " +
lastName + ", " + streetAddress + ", " + zipCode + ", " + city + ", " +
phoneNumber;
return result;
}

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...



void ContactListReader::read(ContactList &mylist)
{
QFile infile(fn);
infile.open(QIODevice::ReadOnly);
QTextStream out(&infile);
while(!infile.atEnd()) {
QString line = infile.readLine();
if(line != "\n") {
QStringList list = line.split(", ");
QString fn = (list.at(1).split(" ")).at(0);
QString ln = (list.at(1).split(" ")).at(1);
mylist.add(Contact((list.at(0)).toInt(),fn,ln,list .at(2),list.at(3),list.at(4),list.at(5)));
}
}
infile.close();
}

Should I be looking beyond these items for a solution? An included file etc?

SixDegrees
8th April 2011, 18:19
"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?

april26
8th April 2011, 19:42
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.

SixDegrees
8th April 2011, 19:50
What happens when you print out the extracted strings (fn, ln) as you process the file?

april26
8th April 2011, 20:03
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...

Zlatomir
8th April 2011, 20:03
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)

april26
8th April 2011, 21:15
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 :-)