[SOLVED] Problem with QFile class, please I need your help
-
Please help me with my problem :
I am trying to write in a text file with the QFile class of Qt, when I save the first time, no problem, but when I save the next time and the file exist, the file is blank, why is that ?... I attach the code ...@void frmAlumnos::ctrlFile(const QString &nomArc, QString idReg)
{
QFile ctrlFile(nomArc);
if(ctrlFile.exists()){
ctrlFile.open(QIODevice::Text | QIODevice::ReadOnly);
QTextStream txtstr(&ctrlFile);
QString sli = txtstr.readAll();
QString sli2;
QStringList cad = sli.split("\n");ctrlFile.flush(); ctrlFile.close(); //sli+=idReg+"\n"; ctrlFile.open(QIODevice::Text | QIODevice::WriteOnly); if (!ctrlFile.isOpen()){ return; }else{ QTextStream txtstr1(&ctrlFile); int ctrol=0; for (int i=0;i<cad.size();i++){ if (cad[i].isEmpty()){return;} // If find a blank space if (cad[i]==idReg){ ctrol++; } sli2+=cad[i]+"\n"; } if (ctrol<=0){ sli2+=idReg+"\n"; } txtstr1<<sli2; ctrlFile.flush(); ctrlFile.close(); } }else{ ctrlFile.open(QIODevice::Text | QIODevice::WriteOnly); if(!ctrlFile.isOpen()){return;} QTextStream txtstr(&ctrlFile); txtstr<<idReg+"\n"; ctrlFile.flush(); ctrlFile.close(); }
}@
I will be grateful to you
-
You open a file for write, which means that you delete the content of the file and then you return from the function without writing anything to the file.
File is closed by QFile destructor and it is empty.See line 23 in your example.
@
if (cad[i].isEmpty()){return;} // If find a blank space
@ -
You probably do not need to call QFile::flush().
Maybe your code is hitting line 23 and returning before anything is written into the file? Or maybe you want to append to the file, in which case you need to add QFile::Append flag to QFile::open() call.
-
Heeeeeeeey !!!! really ? this the error on line 23, the @return@ code is wrong, the correct sintax is @continue@, because the @return@ is for exit the function... :-/... Very thanks sierdzio ... it's solution for my problem ...
Regards
-
Heeeeeeeey !!!! really ? this the error on line 23, the @return@ code is wrong, the correct sintax is @continue@, because the @return@ is for exit the function... :-/... Very thanks andreyc ... it's solution for my problem ...
Regards
-
I merged the duplicated threads.
rishardcarra please do not duplicate your questions in the same language.