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