csv file
-
I am writing each time on filetmp1 the problem is that when I close the application and I open it again the code will delete the old data
code :
//open csv1 if(!filetmp.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"error opening:"<<filetmp.fileName(); return; } //open csv2 QFile filetmp1("c:/Users/LattePanda/Desktop/MOBILTOX/Paramétre_tmp1.csv"); // Vérifiaction l'exsitance de fichier d'indentification if(!filetmp1.open(QFile::ReadWrite | QFile::Text)) { qDebug()<<"error opening:"<<filetmp1.fileName(); return; } // read header csv1 QString h1=filetmp.readLine(); h1=h1.trimmed(); // get rid of ending /n qDebug()<<h1; // read header csv2 QString h2=filetmp1.readLine(); h2=h2.trimmed(); qDebug()<<h2; // get field lists QStringList h1List=h1.split(';'); QStringList h2List=h2.split(';'); while(!filetmp.atEnd()) // read lines csv1 { QString line=filetmp.readLine(); line=line.trimmed(); QStringList val1List=line.split(';'); QString val2; for(const QString h : h2List) // browse all csv2 fields { if(h1List.contains(h)) // does it exist in csv1 ? { int index=h1List.indexOf(h); // at what index val2.append(val1List.at(index)); // write value } if(h!=h2List.last()) val2.append(';'); } val2.append("\n"); qDebug()<<val2; filetmp1.write(val2.toUtf8()); } filetmp.close(); filetmp1.close();
-
@micheal15 said in csv file:
filetmp.open(QFile::ReadOnly | QFile::Text)
Basics, open in append mode:
filetmp.open(QIODeviceBase::ReadOnly | QIODeviceBase::Text | QIODeviceBase::Append)
-
@micheal15
Your code does a read fromfiletmp1
and later writes to it. Personally I do not like reading from a file and then appending to it without re-opening. If you must I would callseek()
before writing to it, a common restriction of read/write IO is that you must seek after changing between reading and writing.I do not know whether Qt's
QFile::Append
file open flag guarantees to do this for you. Nor how it interacts withQFile::ReadWrite
. Nor what you expect/want to happen if the destination file already has anything more in it than the header line you read. -
@micheal15 said in csv file:
but it write nothing on this file
Then debug your code, there is a reason why there are debugers. What does open(...) return?