-1

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.

coder
  • 52
  • 1
  • 6
  • For C++ streams, `while (!file.eof())` [is wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). I would imagine the same is true for `while (!file.atEnd())`. – Some programmer dude Jan 13 '22 at 07:41
  • As for your problem, the `"v1"` lines, is the first element *exactly* equal to `"v1"` (case-sensitive) or do the first column *begin* with or only *contain* `"v1"`? How many of these `"v1"` lines are there? – Some programmer dude Jan 13 '22 at 07:46
  • in 1st column and first row only contain "v1". after then it start sequence number. – coder Jan 13 '22 at 07:48

1 Answers1

0

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

write.close();
coder
  • 52
  • 1
  • 6