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. Random lines in QFile
Qt 6.11 is out! See what's new in the release blog

Random lines in QFile

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

    Hello guys. Here we go again...

    I have a Text File and I'm reading from him 3 lines.

     QFile file(":/sources/expenses.txt");
        {
            if (file.open(QIODevice::ReadOnly | QIODevice::Text))
            {
                QTextStream in(&file);
                in.setCodec("UTF-8");
    
                while(!in.atEnd())
                {
                    ui->DrawTitle->setText(in.readLine());
                    ui->DrawDescrip->setText(in.readLine());
                    ui->DrawCash->setText(in.readLine());
                }
    
                file.close();
            }
        }
    

    I want to read three other, random lines every use of this function. For example:

    //TEXT FILE
    1 line
    2 line
    3 line
    4 line 
    5 line
    6 line
    

    Running the read function reads first lines (1 line, 2 line, 3 line) and other one time she reads (4 line, 5 line, 6 line) e.t.c

    Can you help me?

    jsulmJ 1 Reply Last reply
    0
    • naaxN naax

      Hello guys. Here we go again...

      I have a Text File and I'm reading from him 3 lines.

       QFile file(":/sources/expenses.txt");
          {
              if (file.open(QIODevice::ReadOnly | QIODevice::Text))
              {
                  QTextStream in(&file);
                  in.setCodec("UTF-8");
      
                  while(!in.atEnd())
                  {
                      ui->DrawTitle->setText(in.readLine());
                      ui->DrawDescrip->setText(in.readLine());
                      ui->DrawCash->setText(in.readLine());
                  }
      
                  file.close();
              }
          }
      

      I want to read three other, random lines every use of this function. For example:

      //TEXT FILE
      1 line
      2 line
      3 line
      4 line 
      5 line
      6 line
      

      Running the read function reads first lines (1 line, 2 line, 3 line) and other one time she reads (4 line, 5 line, 6 line) e.t.c

      Can you help me?

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

      @naax If all lines have the same length you can use https://doc.qt.io/qt-5/qfiledevice.html#seek to go to the line you want to read.
      Or you simply read whole file into a QStringList where each line is a string, then you can access the lines like you want.

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

      naaxN 1 Reply Last reply
      4
      • jsulmJ jsulm

        @naax If all lines have the same length you can use https://doc.qt.io/qt-5/qfiledevice.html#seek to go to the line you want to read.
        Or you simply read whole file into a QStringList where each line is a string, then you can access the lines like you want.

        naaxN Offline
        naaxN Offline
        naax
        wrote on last edited by
        #3

        @jsulm

        I'll try this. Thanks

        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