Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFile Append mode weird behavior
Forum Updated to NodeBB v4.3 + New Features

QFile Append mode weird behavior

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hjunior
    wrote on last edited by
    #1

    Hello. I am using QFile and QTextStream to append lines to a local file on lubuntu linux. I open the file at different times, only same thread. For example user interaction causes modification on this file at 10am then below code works. And at 10.20 am same thing and so on.

    Somehow there are many \00\00\00\00.... bytes written in my files, unfortunately sometimes. It is very interesting that the same problem occurs in log files created by QsLog also which we didnt wrote. Anyway, Here is my code snippet.

        QFile localFile("/home/foo/bar.txt");
        bool hasLocal = localFile.open(QIODevice::Append);
        if (!hasLocal){
            return 0;
        }
        if (localFile.size()==0)//SIFIR DOSYA
            QLOG_WARN() << "New bar.txt created at  " << localPath;
    
        QTextStream localOut(&localFile);
        Business::applyTextCodec(&localOut);//actually stream->setCodec(QTextCodec::codecForName("CP857");
        for (int i=0;i<lines->size();i++){
            if (lines->at(i).trimmed().isEmpty())
                continue;
            QString tmp = QString("1,").append(QString::number(startLineNumber++).rightJustified(5,'0')).append(",");
            tmp.append(lines->at(i));
            tmp.append(TEXTFILE_CRLF);
            localOut << tmp;
        }
        localOut.flush();
        QApplication::processEvents();//not sure to do this
        localFile.close();
        QApplication::processEvents();//not sure to do this
    

    Any ideas?

    jsulmJ 1 Reply Last reply
    0
    • H Hjunior

      Hello. I am using QFile and QTextStream to append lines to a local file on lubuntu linux. I open the file at different times, only same thread. For example user interaction causes modification on this file at 10am then below code works. And at 10.20 am same thing and so on.

      Somehow there are many \00\00\00\00.... bytes written in my files, unfortunately sometimes. It is very interesting that the same problem occurs in log files created by QsLog also which we didnt wrote. Anyway, Here is my code snippet.

          QFile localFile("/home/foo/bar.txt");
          bool hasLocal = localFile.open(QIODevice::Append);
          if (!hasLocal){
              return 0;
          }
          if (localFile.size()==0)//SIFIR DOSYA
              QLOG_WARN() << "New bar.txt created at  " << localPath;
      
          QTextStream localOut(&localFile);
          Business::applyTextCodec(&localOut);//actually stream->setCodec(QTextCodec::codecForName("CP857");
          for (int i=0;i<lines->size();i++){
              if (lines->at(i).trimmed().isEmpty())
                  continue;
              QString tmp = QString("1,").append(QString::number(startLineNumber++).rightJustified(5,'0')).append(",");
              tmp.append(lines->at(i));
              tmp.append(TEXTFILE_CRLF);
              localOut << tmp;
          }
          localOut.flush();
          QApplication::processEvents();//not sure to do this
          localFile.close();
          QApplication::processEvents();//not sure to do this
      

      Any ideas?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Hjunior Those 0 bytes could come from Unicode characters - are you writing Unicode?
      You could add

      qDebug() << tmp;
      

      to your loop to see in the console what you're writing into the file.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • H Offline
        H Offline
        Hjunior
        wrote on last edited by
        #3

        No. All of the strings i write is ENGLISH chars.

        And the \00 bytes are appended after my append finished and file closed. I mean i write 5 lines (Block A). No problem. Then 2 minutes later i open the file again and write some lines again (Block B) and so on (Block C,D,E). At this point i realized sometimes some \00 chars appended before i reopen it. Like;
        A B \00\00\00... C D E F \00\00 G H....

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Did you try to print the line content before you pass it to the stream ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved