Home > Mobile >  How to read between lines in a text file?
How to read between lines in a text file?

Time:11-16

I have a txt file with 20 lines. I want to output from line 10 - 20 on a QTextBrowser.

I did something like this:

`
QFile file(name);

file.open(QIODevice::ReadOnly|QIODevice::Text);

QTextStream instream(& file);

 

int linecount_1 = 10;

        while(linecount_1<=20)
        {
            QString line = instream.readLine();
            ui->textBrowser->append(line);
              linecount_1;
        }



file.close();

I am expecting this to read from line 10 to 20 but I'm wrong this is reading from line 1 to 10!

Can anyone spot what mistake I'm doing here?

CodePudding user response:

You can't just read lines starting from line 10; you have to read all the lines and add a if condition which decides if you want to drop the read input in case you're reading lines >10 or you want to append it in case you're reading lines between 10 and 20.

int linecount_1 = 0;

        while(linecount_1<20)
        {
            QString line = instream.readLine();

            if (linecount_1 >= 10)
            {
                 ui->textBrowser->append(newline);
            }
            
            
              linecount_1;
        }
  •  Tags:  
  • qt
  • Related