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. Write content from old file to new file in steps
Forum Updated to NodeBB v4.3 + New Features

Write content from old file to new file in steps

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 228 Views
  • 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.
  • V Offline
    V Offline
    viniltc
    wrote on last edited by
    #1

    Hi,

    I want to write content from an old file to a new file in two steps:

    Step 1: copy first 24 lines from old file to the new file as it is.
    Step 2: Read line by line from 25 do some modification in each lines and write to the new file

    I did as follows:

    Old file opened in Read Only mode , copied contents to QTextStream instream
    Opened the file to be written in ReadWrite mode and there are two while loops

    QString oldfilename = "log_file";
        QString newfilename = "new_log_file";
    
        QString path1 = QCoreApplication::applicationDirPath()+"/"+oldfilename +".txt";
        QString path2 = QCoreApplication::applicationDirPath()+"/"+newfilename+".txt";
    
        QFile readfile(path1);
        if(!readfile.open(QIODevice::ReadOnly | QIODevice::Text)){
    
            qDebug() << "Error opening file: "<<readfile.errorString();
        }
    
        QTextStream instream(& readfile);
      
    

    Next, I opened the file to be written in ReadWrite mode, with two while loops. The first while loop writes the first 24 lines into the new file, and that works.

    The problem is with the 2nd while loop , that is meant to read QTextStream instream from line 24 , line by line. I've if statements to go through each line and show it in the qDebug. But that not showing the right lines!

    I feel like it's problem I defined in the 2nd while loop but not sure what exactly to be done.

    
        QFile writefile(path2);
        if(file.open(QIODevice::ReadWrite | QIODevice::Text))
        {
           
     QTextStream outstream(& file);
    
            int linecount_1 = 0;
    
            while(linecount_1<24){
    
                QString line = instream.readLine();
    
                outstream <<line<< '\n';
                linecount_1++;
                } // <-------upto this write on a new file 
    
    
    
            int linecount_2 = 0
    
            while(!instream.atEnd())
            {
                QString line = instream.readLine();
                
               if(nb_line == 24)
                {
                   qDebug () << line ; // this is not showing the right line
                }
                if(nb_line == 25)
                {
                    qDebug () << line ;
                }
    
                if(nb_line == 26 )
                {
                   qDebug () << line ;
                }
                
            ++linecount_2;
           
              }
            
         readfile.close();
         writefile.close();
        }
    

    can someone spot issues in the implementation?

    jsulmJ 1 Reply Last reply
    0
    • V viniltc

      Hi,

      I want to write content from an old file to a new file in two steps:

      Step 1: copy first 24 lines from old file to the new file as it is.
      Step 2: Read line by line from 25 do some modification in each lines and write to the new file

      I did as follows:

      Old file opened in Read Only mode , copied contents to QTextStream instream
      Opened the file to be written in ReadWrite mode and there are two while loops

      QString oldfilename = "log_file";
          QString newfilename = "new_log_file";
      
          QString path1 = QCoreApplication::applicationDirPath()+"/"+oldfilename +".txt";
          QString path2 = QCoreApplication::applicationDirPath()+"/"+newfilename+".txt";
      
          QFile readfile(path1);
          if(!readfile.open(QIODevice::ReadOnly | QIODevice::Text)){
      
              qDebug() << "Error opening file: "<<readfile.errorString();
          }
      
          QTextStream instream(& readfile);
        
      

      Next, I opened the file to be written in ReadWrite mode, with two while loops. The first while loop writes the first 24 lines into the new file, and that works.

      The problem is with the 2nd while loop , that is meant to read QTextStream instream from line 24 , line by line. I've if statements to go through each line and show it in the qDebug. But that not showing the right lines!

      I feel like it's problem I defined in the 2nd while loop but not sure what exactly to be done.

      
          QFile writefile(path2);
          if(file.open(QIODevice::ReadWrite | QIODevice::Text))
          {
             
       QTextStream outstream(& file);
      
              int linecount_1 = 0;
      
              while(linecount_1<24){
      
                  QString line = instream.readLine();
      
                  outstream <<line<< '\n';
                  linecount_1++;
                  } // <-------upto this write on a new file 
      
      
      
              int linecount_2 = 0
      
              while(!instream.atEnd())
              {
                  QString line = instream.readLine();
                  
                 if(nb_line == 24)
                  {
                     qDebug () << line ; // this is not showing the right line
                  }
                  if(nb_line == 25)
                  {
                      qDebug () << line ;
                  }
      
                  if(nb_line == 26 )
                  {
                     qDebug () << line ;
                  }
                  
              ++linecount_2;
             
                }
              
           readfile.close();
           writefile.close();
          }
      

      can someone spot issues in the implementation?

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

      @viniltc said in Write content from old file to new file in steps:

      if(nb_line == 24)
      {
      qDebug () << line ; // this is not showing the right line
      }
      if(nb_line == 25)
      {
      qDebug () << line ;
      }

              if(nb_line == 26 )
              {
                 qDebug () << line ;
              }
      

      I'm not sure what this is supposed to do?
      You're not changing nb_line inside that while loop...
      I also don't know why you need these checks at all: if you read 24 lines in the first while loop, then in the second you will read lines starting with line 25.

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

      V 1 Reply Last reply
      2
      • jsulmJ jsulm

        @viniltc said in Write content from old file to new file in steps:

        if(nb_line == 24)
        {
        qDebug () << line ; // this is not showing the right line
        }
        if(nb_line == 25)
        {
        qDebug () << line ;
        }

                if(nb_line == 26 )
                {
                   qDebug () << line ;
                }
        

        I'm not sure what this is supposed to do?
        You're not changing nb_line inside that while loop...
        I also don't know why you need these checks at all: if you read 24 lines in the first while loop, then in the second you will read lines starting with line 25.

        V Offline
        V Offline
        viniltc
        wrote on last edited by
        #3

        @jsulm Thanks , I forgot to start second while loop with the line 25, now it works

        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