I'm making a method (SLOT) with Qt, and got crash, when I lunch this self-writen code.
I really can't find mistake. I hope it's somewhere in loops or maybe there is some secret sign.
QString data_elements[13];
QString fileName = "C:\\Users\\cp1000\\Documents\\msl_register\\data.csv";
QFile inputFile(fileName);
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
int elementId = 0;
while (!in.atEnd())
{
for(int i = 0; i < 13; i++){
data_elements[i] = "";
}
QString line = in.readLine();
for(int i = 0; i < line.length(); i++)
{
if(line[i] == ","){
elementId++;
i++;
}
if(line[i] == "\n"){
elementId = 0;
}
data_elements[elementId] = data_elements[elementId] + line[i];
}
}
inputFile.close();
}
It takes data from data.csv file, which look this.
My task is: Make method that gets and find row in data.csv, and than prints in GUI.
My data.csv file.
Reference,Batch,MSL,Open Date,Open Time,Close Date,Close Time,Drying start date,Drying start time,Spent,Left,Status
0028027,1231,1,07/12/2016,10:13,08/12/2016,15:41,13/12/2015,15:41,0,Neierobeюots,1
0028028,123123,1,07/12/2016,10:37,08/12/2016,15:45,13/12/2016,10:24,0,Neierobeюots,2
0028028,55554444,1,31/01/2017,15:26,08/12/2016,10:19,08/12/2016,15:41,0,Neierobeюots,2
XC0182,456,1,07/12/2016,09:27,08/12/2016,09:37,08/12/2016,15:41,0,Neierobeюots,3
VD0057-MSD,5999,5,15/12/2016,15:28,08/12/2016,13:33,13/12/2016,11:33,0,72,3
XC0182,555444555,1,07/12/2016,13:38,08/12/2016,13:38,08/12/2016,15:41,0,Neierobeюots,1
0028028,1232,2a,07/12/2016,10:14,08/12/2016,15:42,13/12/2016,15:42,1,Neierobeюots,2
0028029,123124,2a,07/12/2016,10:38,08/12/2016,15:46,13/12/2017,10:25,1,Neierobeюots,3
0028029,55554445,3,07/12/2016,10:20,08/12/2016,10:20,19/12/2016,12:52,1,Neierobeюots,3
XC0183,457,2,07/12/2016,09:28,08/12/2016,09:38,08/12/2017,15:42,1,Neierobeюots,4
VD0058-MSD,6000,4,07/12/2016,11:34,08/12/2016,13:34,13/12/2017,11:34,1,73,3
XC0183,555444556,2,07/12/2016,13:39,08/12/2016,13:39,08/12/2017,15:42,1,Neierobeюots,2
154-199-00-011,544325145,3,31/01/2017,13:58,31/01/2017,13:57,0,0,0,168,2
154-199-00-011,7777,3,31/01/2017,14:05,31/01/2017,14:05,0,0,0,168,1
You comes out of array bounds at:
data_elements[elementId] = data_elements[elementId] + line[i];
It happens because line
doesn't contain \\n
at the end of the string and elementId
is never reset to 0
at:
if(line[i] == "\n"){
elementId = 0; // <-- This code never reached
}
Documentation of the QTextStream::readLine
says:
The returned line has no trailing end-of-line characters ("\\n" or "\\r\\n")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.