简体   繁体   English

Qt循环崩溃

[英]Qt Cycle Crashes

I'm making a method (SLOT) with Qt, and got crash, when I lunch this self-writen code. 我用Qt制作了一个方法(SLOT),当我吃掉这个自写的代码时崩溃了。

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. 它从data.csv文件中获取数据,看起来像这样。 Calc中的data.csv

My task is: Make method that gets and find row in data.csv, and than prints in GUI. 我的任务是:在data.csv中获取并查找行的Make方法,然后在GUI中打印。

My data.csv file. 我的data.csv文件。

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: 发生这种情况是因为在字符串的结尾line不包含\\n ,并且在elementId位置永不将elementId重置为0

if(line[i] == "\n"){
  elementId = 0;    // <-- This code never reached
}

Documentation of the QTextStream::readLine says: QTextStream::readLine文档说:

The returned line has no trailing end-of-line characters ("\\n" or "\\r\\n") 返回的行没有尾随行尾字符(“ \\ n”或“ \\ r \\ n”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM