Results 1 to 2 of 2

Thread: How do I correctly use QTextStream to read individual lines?

  1. #1
    Join Date
    Oct 2014
    Posts
    2
    Qt products
    Qt5

    Default How do I correctly use QTextStream to read individual lines?

    Having a bit of trouble here, and can't find relevant functions to do what I need. I have a text file, a big list of words, with nothing separating them except each being on its own line. I want to read a line, do something with it, and then grab the next line, and basically repeat this until the end of the file is reached. I'm sure this is simpler than I am making it out to be but I am not very familiar with file-read functions

    Qt Code:
    1. void Read(QString Filename)
    2. {
    3. QFile mFile(Filename);
    4. if(!mFile.open(QFile::ReadOnly | QFile::Text))
    5. {
    6. return;
    7.  
    8. qDebug() << "could not open file for reading";
    9. }
    10.  
    11. QTextStream in(&mFile);
    12. QString mText = in.readLine();
    13. //Do something with string
    14. qDebug() << mText;
    15. mFile.flush();
    16. mFile.close();
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 



    So this little function (thanks to VoidRealms) can read the very first line of my file. I am not sure how to delete that line from the text stream and then read the next one.

    Would it be better to do a readAll() into a string, and then work from that string? I intend for this particular data to be opened once when the file is executed, and then be called upon when needed, until the program is finished.

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

    Default Re: How do I correctly use QTextStream to read individual lines?

    You do not "delete the line from text stream" as it is a stream - once you read it, it is "gone". In your case you just need to repeat the whole reading line process until you run out of lines. QTextStream docs even contain an example of doing that, there is no need to look for help on external sites, just read your docs.
    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.


Similar Threads

  1. Replies: 1
    Last Post: 28th September 2013, 10:02
  2. QTextStream does not read from stdin
    By MasterBLB in forum Qt Programming
    Replies: 5
    Last Post: 20th November 2012, 18:05
  3. QTextStream cannot read £ characters?
    By KjellKod in forum Qt Programming
    Replies: 10
    Last Post: 15th August 2011, 14:58
  4. Replies: 2
    Last Post: 10th June 2011, 14:16
  5. Read/Write from QTextStream
    By tonde in forum Qt Programming
    Replies: 7
    Last Post: 31st July 2008, 16:51

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.