PDA

View Full Version : QFile reading problem



giblit
5th April 2013, 00:02
write:

QFile file(QDir::homePath() + "/datedata.txt");
if (file.open(QIODevice::WriteOnly |QIODevice::Text | QIODevice::Append)) {
QTextStream out(&file);
out << timeCount << endl;
out << dateEdit->date().toString("MMMM dd, yyyy") << endl;
for(int j = 0; j<timeCount; j++){
out << timeEditList[j]->text() << endl;
out << textEditList[j]->toPlainText() << endl;
for(int k = 0; k<7; k++){
out << numbers[j][k] << endl;
}
}

}
file.close();

read:



QFile file(QDir::homePath() + "/datedata.txt");
if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
QTextStream in(&file);
while(!in.atEnd()){
numberOfTimes << in.readLine().toInt();
dates << in.readLine();
for(int j = 0; j<numberOfTimes.size(); j++){
times << in.readLine();
foods << in.readLine();
for(int k = 0; k<7; k++){
totalValues << in.readLine();
}
}
}
}
file.close();

I used these couts to write what its reading


for(int i = 0; i<numberOfTimes.size(); i++){
cout << "number of times " << i << ": " << numberOfTimes[i] << endl;
}
for(int i = 0; i<dates.size(); i++){
cout << "date " << i << ": " << dates[i].toStdString() << endl;
}
for(int i = 0; i<times.size(); i++){
cout << "time " << i << ": " << times[i].toStdString() << endl;
cout << "foods " << i << ": " << foods[i].toStdString() << endl;
for(int j = 0; j<7; j++){
cout << "total values " << j << ": " << totalValues[j+(7*i)].toStdString() << endl;
}
}
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:

2
April 04, 2013
2:54:33 PM
Apple, Orange
1
1
1
1
1
1
1
2:54:43 PM
Candy
1.27
12.5
8
9.04
21
3
81


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.

amleto
5th April 2013, 00:15
use your debugger. Also read my sig.

giblit
5th April 2013, 00:21
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

for(int i = 0; i<numberOfTimes.size(); i++)

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

int readSize = 0;
QFile file(QDir::homePath() + "/datedata.txt");
if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
QTextStream in(&file);
while(!in.atEnd()){
numberOfTimes << in.readLine().toInt();
dates << in.readLine();
for(int i = 0; i<numberOfTimes[readSize]; i++){
times << in.readLine();
foods << in.readLine();
for(int j = 0; j<7; j++){
totalValues << in.readLine();
}
}
readSize++;
}
}
file.close();

amleto
5th April 2013, 00:40
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.

giblit
5th April 2013, 01:01
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

ChrisW67
5th April 2013, 06:10
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.

giblit
5th April 2013, 06:45
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