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. With QTextStream streamOut and streamOut <<data stop over 316 lines in text file?
Forum Updated to NodeBB v4.3 + New Features

With QTextStream streamOut and streamOut <<data stop over 316 lines in text file?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 866 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.
  • F Offline
    F Offline
    filipdns
    wrote on last edited by
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • F filipdns

      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

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

      @filipdns You again do the same mistake as in https://forum.qt.io/topic/102498/using-qtextstream-to-write-in-textfile-what-is-copied-after-enable-the-the-line-to-copy-is-not-what-i-have-with-qdebug-we-the-line-disable/5

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

      F 1 Reply Last reply
      2
      • jsulmJ jsulm

        @filipdns You again do the same mistake as in https://forum.qt.io/topic/102498/using-qtextstream-to-write-in-textfile-what-is-copied-after-enable-the-the-line-to-copy-is-not-what-i-have-with-qdebug-we-the-line-disable/5

        F Offline
        F Offline
        filipdns
        wrote on last edited by filipdns
        #3

        @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();
        }
        
        jsulmJ 1 Reply Last reply
        0
        • F filipdns

          @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();
          }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @filipdns You read and write same file at the same time
          QString list = streamOut.readLine();
          streamOut <<streamOut2.split(";")[0]+"\n";

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

          F 1 Reply Last reply
          2
          • jsulmJ jsulm

            @filipdns You read and write same file at the same time
            QString list = streamOut.readLine();
            streamOut <<streamOut2.split(";")[0]+"\n";

            F Offline
            F Offline
            filipdns
            wrote on last edited by
            #5

            @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...

            jsulmJ 1 Reply Last reply
            0
            • F filipdns

              @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...

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

              @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

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

              F 1 Reply Last reply
              4
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                you can use QSaveFile to easily achieve

                Open a temporary file (lets call it B) in write only mode
                Delete A
                Move/rename B so that it becomes A

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                F 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @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
                  F Offline
                  F Offline
                  filipdns
                  wrote on last edited by
                  #8

                  @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();
                  }
                  
                  1 Reply Last reply
                  0
                  • VRoninV VRonin

                    you can use QSaveFile to easily achieve

                    Open a temporary file (lets call it B) in write only mode
                    Delete A
                    Move/rename B so that it becomes A

                    F Offline
                    F Offline
                    filipdns
                    wrote on last edited by
                    #9

                    @VRonin Thank I will try that too

                    1 Reply Last reply
                    0

                    • Login

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