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

Get single line from QFile

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 667 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.
  • C Offline
    C Offline
    Creatorczyk
    wrote on last edited by
    #1

    Hi,

    I would like to read a single line at position 5 from the QFile object and write it to QString. How could I do that?

    jsulmJ 1 Reply Last reply
    0
    • C Creatorczyk

      Hi,

      I would like to read a single line at position 5 from the QFile object and write it to QString. How could I do that?

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

      @Creatorczyk https://doc.qt.io/qt-5/qfiledevice.html#seek
      https://doc.qt.io/qt-5/qiodevice.html#readLine-1

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

      C 1 Reply Last reply
      3
      • jsulmJ jsulm

        @Creatorczyk https://doc.qt.io/qt-5/qfiledevice.html#seek
        https://doc.qt.io/qt-5/qiodevice.html#readLine-1

        C Offline
        C Offline
        Creatorczyk
        wrote on last edited by
        #3

        @jsulm I wrote this code to get 2 line from below file

        file.txt

        abcdef
        ghijkl
        mnoprs
        tuvwxy
        

        My code:

        file.seek(2);
        QByteArray line = file.readLine();
        qDebug()<< QString(line);
        

        But my output is:

        cdef
        

        What could I do to get below line?

        mnoprs
        
        J.HilkJ jsulmJ 2 Replies Last reply
        0
        • C Creatorczyk

          @jsulm I wrote this code to get 2 line from below file

          file.txt

          abcdef
          ghijkl
          mnoprs
          tuvwxy
          

          My code:

          file.seek(2);
          QByteArray line = file.readLine();
          qDebug()<< QString(line);
          

          But my output is:

          cdef
          

          What could I do to get below line?

          mnoprs
          
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Creatorczyk question, do you know, the exact length of your each line ? And do you know the end of line format ?

          If the answer is yes, then you simply used seek false.

          seek about = bytes in line * 2 to get to the start of line 3

          if you don't know it, or it may vary, you'll have to use
          https://doc.qt.io/qt-5/qiodevice.html#readLine-1

          and count how often you read a line, discard everything before the count of 3


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • C Creatorczyk

            @jsulm I wrote this code to get 2 line from below file

            file.txt

            abcdef
            ghijkl
            mnoprs
            tuvwxy
            

            My code:

            file.seek(2);
            QByteArray line = file.readLine();
            qDebug()<< QString(line);
            

            But my output is:

            cdef
            

            What could I do to get below line?

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

            @Creatorczyk seek() works with bytes, not lines. In your first post you did not say that "position 5" means line 5 and not byte 5. If you want to get the line number n you have to read and skip lines 0..n-1:

            for (int i = 0; i < 2; ++i)
                file.readLine();
            QString line = file.readLine();
            

            Keep in mind: this code does not have any error handling (you could have less lines in the file than you expect).

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

            1 Reply Last reply
            3

            • Login

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