Hi all,

I use Qt 4.8.2 on WindowsXP, VS2010.

It seems I found a bug in QFile during appending text. Long time ago I wrote a simple "Trace" class for writing data (text, xml) based on Qt 4.7.8. Never a problem occured.

But now with Qt 4.8.2 the content of the file is confused. My "Trace" class normally writes line by line with a timestamp at beginning and appends a new line at the end. But sometimes whole lines missing or older lines at beginning of the file are overwritten.

A simple example is to write a "socket connection state" into the file. Inside the slots of connected/disconnected/errorOccured I write a line to the trace.

Here is a code snipped of my Trace::write() method, the member m_file is a QFile:
Qt Code:
  1. void Trace::write(const QString text)
  2. {
  3. if (!m_enabled)
  4. return;
  5.  
  6. QString filename = getFilenameToday();
  7.  
  8. if (filename.isEmpty())
  9. return;
  10.  
  11. QString str;
  12. QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz");
  13.  
  14. if (!QFile::exists(filename))
  15. {
  16. str = QString("************************** Startup %1 **************************\n")
  17. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
  18. }
  19.  
  20. str += "\n" + timestamp + ": " + text;
  21. str.replace("\r\n", "\n");
  22.  
  23.  
  24. m_file.setFileName(filename);
  25.  
  26. if (m_file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text | QIODevice::Unbuffered))
  27. {
  28. m_file.write(str.toLatin1());
  29. m_file.flush();
  30. m_file.close();
  31. }
  32.  
  33. deleteOldFiles();
  34. }
To copy to clipboard, switch view to plain text mode 

I now often opening and closing a file is time consuming but this allows to me to open/copy/delete the file during my application is running.

Have anybody an idea what's going wrong? I looked into the changes of 4.8.1 and 4.8.2 and there were fixes of QFile and QIODevice. Maybe they fixed one bug and added a new one ;-)

Thanks a lot...