Solved With QTextStream streamOut and streamOut <<data stop over 316 lines in text file?
-
hello,
I'm filling text file with data and it's working fine until 315 lines but after that, all data after just replace the 316's line but never pass to the 317's line, do you know why?...
here the code:
void MainWindow::readData(const QByteArray &data) { QFile fileOut("C:/Magasin_Temp_Hygro.txt"); fileOut.open(QIODevice::ReadWrite | QIODevice::Text); QTextStream streamOut(&fileOut); QString delimiter=QString(data.at(data.size()-1)); if(delimiter==";") { QStringList result=streamOut2.split(";"); QString list = streamOut.readLine(); QDateTime timestamp; timestamp=QDateTime::currentDateTime(); QString time=timestamp.toString("dd/MM/yy hh'h'mm"); streamOut2=time+" "+streamOut2.replace("\n","").replace("\r",""); if(streamOut2.size()<52) { streamOut <<streamOut2.split(";")[0]+"\n"; line=streamOut2.split(";")[0]; } streamOut2=""; fileOut.copy("C:/Magasin_Temp_Hygro.txt","C:/Temp_Hygro.txt"); }else{ streamOut2=streamOut2+QString(data); } fileOut.close(); }
thank you for your help
-
-
@jsulm Hello, what mistake? I'm just copy the file, I try to disable any other use of the file including copy but same result... And why the problem appear always over 315 lines if the problem is from the read / write?...
void MainWindow::readData(const QByteArray &data) { QFile fileOut("//DGS1109N007/echange_hangar/soft/Magasin/Magasin_Temp_Hygro.txt"); fileOut.open(QIODevice::ReadWrite | QIODevice::Text); QTextStream streamOut(&fileOut); QString delimiter=QString(data.at(data.size()-1)); if(delimiter==";") { QStringList result=streamOut2.split(";"); QString list = streamOut.readLine(); QDateTime timestamp; timestamp=QDateTime::currentDateTime(); QString time=timestamp.toString("dd/MM/yy hh'h'mm"); streamOut2=time+" "+streamOut2.replace("\n","").replace("\r",""); if(streamOut2.size()<100) { streamOut <<streamOut2.split(";")[0]+"\n"; line=streamOut2.split(";")[0]; } streamOut2=""; }else{ streamOut2=streamOut2+QString(data); } fileOut.close(); }
-
@filipdns You read and write same file at the same time
QString list = streamOut.readLine();
streamOut <<streamOut2.split(";")[0]+"\n"; -
@jsulm oh, ok, but without QString list = streamOut.readLine(); , the value is write on one line and each other value is copied over this line not on new line...
-
@filipdns You should do the following:
- Open original file you want to change (lets call it A) in read only mode
- Open a temporary file (lets call it B) in write only mode
- Read from A, modify as needed and write to B
- When finished close both files
- Delete A
- Move/rename B so that it becomes A
-
you can use
QSaveFile
to easily achieveOpen a temporary file (lets call it B) in write only mode
Delete A
Move/rename B so that it becomes A -
@jsulm I did what you say, I's working fine, thank a lot!!
void MainWindow::readData(const QByteArray &data) { QFile fileIn("//DGS1109N007/echange_hangar/soft/Magasin/Magasin_Temp_Hygro.txt"); fileIn.open(QIODevice::ReadOnly | QIODevice::Text); QTextStream streamIn(&fileIn); QByteArray inputData = fileIn.readAll(); QString delimiter=QString(data.at(data.size()-1)); if(delimiter==";") { QString list = streamIn.readLine(); QFile::copy("//DGS1109N007/echange_hangar/soft/Magasin/Magasin_Temp_Hygro.txt","//DGS1109N007/echange_hangar/soft/Magasin/Magasin_Temp_Hygro_temp.txt"); QFile fileOut("//DGS1109N007/echange_hangar/soft/Magasin/Magasin_Temp_Hygro_temp.txt"); if(!fileOut.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug()<<"Error Magasin_Temp_Hygro_temp.txt not open"; } QTextStream streamOut(&fileOut); streamOut <<inputData+"\n"; QStringList result=streamOut2.split(";"); QDateTime timestamp; timestamp=QDateTime::currentDateTime(); QString time=timestamp.toString("dd/MM/yy hh'h'mm"); streamOut2=time+" "+streamOut2.replace("\n","").replace("\r",""); if(streamOut2.size()<100) { streamOut <<streamOut2.split(";")[0]; line=streamOut2.split(";")[0]; fileOut.close(); fileIn.close(); QFile::remove("//DGS1109N007/echange_hangar/soft/Magasin/Magasin_Temp_Hygro.txt"); QFile::rename("//DGS1109N007/echange_hangar/soft/Magasin/Magasin_Temp_Hygro_temp.txt","//DGS1109N007/echange_hangar/soft/Magasin/Magasin_Temp_Hygro.txt"); } streamOut2=""; streamOut.flush(); }else{ streamOut2=streamOut2+QString(data); } streamIn.flush(); }
-
@VRonin Thank I will try that too