Search code examples
c++csvqtcsv-write-streamcsvwriter

Last 52 lines are not written in csv file using qt


I have one csv file in which 3 column and 866300 lines. I have try to write this data into other csv file. when i try to write it has write 866248 lines in file after that remaining 52 lines are not write in file. what is the problem I do not understand it. I have try to debug this problem using print that data on console then it has print till last line on the console. only the problem in write the data in file.

#include <QCoreApplication>
#include <QFile>
#include <QStringList>
#include <QDebug>
#include <QTextStream>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QFile file("C:/Users/hello/Downloads/hello.csv");

    QFile write("new_data.csv");

    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug()<<file.errorString();
        return 1;
    }

    if(!write.open(QIODevice::WriteOnly |QIODevice::Append))
    {
        qDebug()<<file.errorString();
        return 1;
    }

    QTextStream out(&write);

    QStringList data;
    while (!file.atEnd())
    {
        QString line = file.readLine();
        data = line.split(',');
        if (data[0]=="v1")
        {
            out<<line;
            continue;
        }
        else
        {
            int seq = (data[0].toInt())-1;
            QString str = QString::number(seq)+","+data[1]+","+data[2].trimmed();
            qDebug()<<str;
            out<<str<<"\n";
        }
    }
    return a.exec();
}

please help.


Solution

  • Please close the file before the return a.exec(); this line and after the while loop. add this line below line.

    write.close();