1

I am creating a function named getExcelFile() to get the contents of the excel file and store it into an array of structures. First I wanted read one line of the csv file and use the delimiter , . Then I want to add the contents to the QStringList and use a for loop to iterate through an array of structures and add the contents into it. Everything is working fine except for the line where it says datalist.append((line.split(','))); Help would be greatly appreciated!

void Widget::getExcelFile(){
    //Name of the Qfile object.
    //filename is the directory of the file once it has been selected in prompt
    QFile thefile(filename);
    //QStringList named datalist.
    QStringList datalist;
    //If the file is successfully open
    if (thefile.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "File opened successfully";
        while (!thefile.atEnd()){
                    QByteArray line = thefile.readLine();
                    datalist.append((line.split(',')));


                }

    }

    qDebug() << datalist;
    ui->textEdit->setPlainText(fileContent);
}
  • Possible duplicate of: https://stackoverflow.com/questions/27318631/parsing-through-a-csv-file-in-qt – besc Mar 04 '17 at 19:59

1 Answers1

0

CSV files are a bit trickier to read than that.

Your specific file may have simple records, but Excel will create CSVs wth escapes, missing values, and so on.

If you take my CSV parser, you can then query it both for CSV file structure (what columns are in there and what is their type) and for the values. The parser doesn't do anything terribly complicated, but it handles the messiness of allocating memory and breaking the fields out and assigning them to columns.

If you are filling in a simple C structure, you then throw out any CSV objects that don't match. If an object does match, it's a simple loop through with calls to getfield. You then destroy the CSV object.

http://www.malcolmmclean.site11.com/www/CSVtoC/csvtoc.html

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18