Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    How do I correctly use QTextStream to read individual lines?

    General and Desktop
    2
    4
    3039
    Loading More Posts
    • 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.
    • S
      Sirox last edited by

      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

      @void Read(QString Filename)
      {
      QFile mFile(Filename);
      if(!mFile.open(QFile::ReadOnly | QFile::Text))
      {
      return;

       qDebug() << "could not open file for reading";
      

      }

      QTextStream in(&mFile);
      QString mText = in.readLine();
      //Do something with string
      qDebug() << mText;
      mFile.flush();
      mFile.close();

      }
      @

      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.

      1 Reply Last reply Reply Quote 0
      • IamSumit
        IamSumit last edited by

        Hi
        and welcome to Qt devnet
        @
        while(!in.atEnd())
        {
        QString mText = in.readLine();
        //Do something with string
        qDebug() << mText;
        }
        @

        hope this helps.

        Be Cute

        1 Reply Last reply Reply Quote 0
        • S
          Sirox last edited by

          Interesting, thanks.

          Can you explain why that works? I knew how to do a loop like that, but I assumed it would just continually read the first line over and over again. How come it automatically goes to the next line? Does text automatically process the text in a linear forward moving fashion or something?

          1 Reply Last reply Reply Quote 0
          • IamSumit
            IamSumit last edited by

            Hi.
            [quote author="Sirox" date="1412914191"]Interesting, thanks.

            Can you explain why that works? I knew how to do a loop like that, but I assumed it would just continually read the first line over and over again. How come it automatically goes to the next line? Does text automatically process the text in a linear forward moving fashion or something?[/quote]

            When you use readLine().This function reads until a \n(enter) is encountered.
            so it always gives you the first line.
            on the other hand if you want to read all lines of a file then you have to go through all lines of that file. so you can achieve it using while loop.
            atEnd() functions Returns true if the current read and write position is at the end of the device (i.e. there is no more data available for reading on the device); otherwise returns false.
            "here":http://qt-project.org/doc/qt-5/qiodevice.html#atEnd

            hope this helps.

            Be Cute

            1 Reply Last reply Reply Quote 0
            • First post
              Last post