Results 1 to 12 of 12

Thread: Read a specific line from a file

  1. #1
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Read a specific line from a file

    I need to read a specific line from my file.

    My file contains these contents

    <html > one </html>
    <html > two </html>
    <html> three</html>
    I know to read the entire file using QFile,QTextStream.
    But now i want to read the file which starts with <html>. Even if i use readLine() , i can read each and every line of a File. Does anybody know how to read particular line from a File ??????????

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read a specific line from a file

    Yes, read all the lines previous to the line you want and ignore them, then read the line you want and store it. If the lines are the same length then you can seek to a particular position in the file using a simple formula, but they rarely are.

    But if your reading xml (it seems like it from your example), a better idea would be QDomDocument.

  3. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read a specific line from a file

    If your file has XML like contents, you can use QXmlStreamReader or QDomDocument.

    If your file is a flat file (as you described) you can only implement this feature by hands
    A camel can go 14 days without drink,
    I can't!!!

  4. #4
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: Read a specific line from a file

    What about reading a particular Line from a Text file ??????????

  5. #5
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read a specific line from a file

    what does "particular" mean? The n-th line? A line starting with "xyz"?

  6. #6
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: Read a specific line from a file

    ya!! A line starts with "abc " like that!!

  7. #7
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read a specific line from a file

    then you read line by line. you check each line if it starts with "abc" and if it does, you end the loop.

  8. #8
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read a specific line from a file

    The easiest way to read a Text File is using QTextStream.

    You can create a class that inherit from it and add this function.
    Example
    Qt Code:
    1. class MyTextStream: public QTextStream
    2. {
    3. ....
    4. public:
    5. QString readLineStartingWith ( const QString& str, qint64 maxlen = 0 );
    6. };
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Read a specific line from a file

    Quote Originally Posted by mcosta View Post
    The easiest way to read a Text File is using QTextStream.
    No, that's not the easiest way. Especially for a total noob in the topic like the OP.

    @rleojoseph:
    Use readLine() together with QString::startsWith()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read a specific line from a file

    Quote Originally Posted by wysota View Post
    @rleojoseph:
    Use readLine() together with QString::startsWith()
    I agree with you. I suggested a dedicated class if the task of reading line with specific start is common and repeated in his code
    A camel can go 14 days without drink,
    I can't!!!

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Read a specific line from a file

    After sticking here for a couple of years I became acustomed to the fact that newbies don't want/don't know how to subclass. It's an apparent lack of object oriented programming skills but there is nothing we can do about it apart repeating over and over to learn C++ before using Qt.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read a specific line from a file

    I should suggest us to highlight to newbies that a good Qt developer needs to be a good C++ developer
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. Replies: 8
    Last Post: 8th May 2012, 10:39
  2. Replies: 3
    Last Post: 13th August 2010, 12:50
  3. How to read line number in a text file
    By grsandeep85 in forum Qt Programming
    Replies: 7
    Last Post: 31st July 2009, 10:09
  4. how to move cursor to specific line in Text
    By showhand in forum Qt Programming
    Replies: 1
    Last Post: 24th August 2006, 10:55
  5. How to read line from file
    By Krishnacins in forum Newbie
    Replies: 10
    Last Post: 2nd June 2006, 00:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.