Last 52 lines are not written in csv file.
-
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 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 try to debug it using print on console then it has print till last on the console. only the problem in write 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/Liya Dev/Downloads/Patient22.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.
this is the console output.
this is the csv file output.
-
@dev_liya said in Last 52 lines are not written in csv file.:
QString str = QString::number(seq)+","+data[1]+","+data[2].trimmed();
You should check the size of data.
Also running your program in the debugger will show you where exactly it is crashing.
-
Apart from all the stuff above - why do you need to covert
,
to;
- Excel and LibreOffice calc can import any csv file with a self-defined separator. -
@Christian-Ehrlicher What you said i cannot understand it.
-
And your last line do not start with "v1" ?
-
@Christian-Ehrlicher only first row contain v1 after all rows contain data.
-
@dev_liya Then remove all rows except the ones which are not put into the new file and try again. If they still don't get into the new file, please post the content of those lines.
-
@jsulm said in Last 52 lines are not written in csv file.:
@dev_liya I also would close the file before calling "return a.exec()".
@dev_liya
You should definitely do this (correctly). Without closing (or flushing) the file, the last "block" will not have been sent to the file when you do youra.exec()
--- it will still sit in memory. It seems likely this would be the cause of your "Last 52 lines are not written in csv file"? -
@jsulm @Christian-Ehrlicher @JonB
QString str = QString::number(seq)+","+data[1]+","+data[2].trimmed();
i have removed
"trimmed()"
from this line and also remove\n
from this below line.out<<str<<"\n";
then it has been print till 866261. after that not print. what is the problem i cannot under stand.
-
Reduce your file to see if it's the length or the specific line as already suggested
-
@dev_liya said in Last 52 lines are not written in csv file.:
then it has been print till 866261. after that not print. what is the problem i cannot under stand.
Have you acted on closing or flushing the file as suggested, or ignored that?
This is a separate question from why you got or have "The program has unexpectedly finished", which you must resolve if it's still happening. If it happens while writing lines to the file, you will indeed find further lines are missing, you must not get such an error.