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. Reading .txt file info into variables
Forum Updated to NodeBB v4.3 + New Features

Reading .txt file info into variables

Scheduled Pinned Locked Moved General and Desktop
15 Posts 6 Posters 19.1k Views 1 Watching
  • 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 Offline
    S Offline
    sheehan1234
    wrote on last edited by
    #4

    I have QStringList of integers that I read in from a file. How do I iterate through this list to: a) save each value b) cast each to an integer.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #5

      I'm not sure what you mean by "save each value" but iterating through a QStringList is fairly simple. There are a number of ways to do so.

      @
      QStringList list;

      // iterate through each element of list (which will be accessible via the QString "str")
      foreach (QString str, list)
      {
      ...
      int value = str.toInt(); // get integer representation of str
      ...
      }
      @

      BTW, This stuff is very well-documented.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sheehan1234
        wrote on last edited by
        #6

        I meant to say store each into a variable, which you answered. However I'm trying to do so in the following code, and when I try to print the value, nothing is displayed. Am I reading my text file into the QStringList incorrectly?

        @int main()
        {
        QStringList stringList;
        QFile textFile("test.txt");
        QTextStream textStream(&textFile);
        while (true)
        {
        QString line = textStream.readLine();
        if (line.isNull())
        break;
        else
        stringList.append(line);
        }

        foreach(QString str, stringList)
        {
            int value = str.toInt();
            cout << value;
        }
        return 0;
        

        }
        @

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #7

          You need to call "textFile.open()":http://doc.qt.nokia.com/4.7-snapshot/qfile.html#open

          Be sure and check its return value. There's an example in the QFile docs.

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mlong
            wrote on last edited by
            #8

            A cleaner implementation would probably be:

            @
            QStringList stringList;
            QFile textFile("test.txt");
            if (textFile.open(QIODevice::ReadOnly)
            {
            QTextStream textStream(&textFile);
            while (!textStream.atEnd())
            {
            stringList.append(textStream.readLine());
            }
            }
            @

            Caveat: Brain to keyboard. Untested code. Not responsible for silly errors. YMMV

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sheehan1234
              wrote on last edited by
              #9

              that worked! thank you very much!

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #10

                [quote author="sheehan1234" date="1317152090"]
                If that is done correctly, how do I go about iterating through it?[/quote]

                @
                foreach(const QString line, stringList) {
                // do something with the line
                }
                @

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #11

                  This discussion looks a lot like "this":http://developer.qt.nokia.com/forums/viewthread/10146/ one...

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #12

                    [quote author="Andre" date="1317189187"]This discussion looks a lot like "this":http://developer.qt.nokia.com/forums/viewthread/10146/ one...[/quote]

                    I merged both threads.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sheehan1234
                      wrote on last edited by
                      #13

                      I have the code exactly as you all have showed me but there was no output. so i put some print statements inside of the loops to see if the compiler was even entering the loops and it is not. What am I doing wrong?

                      @int main()
                      {
                      QStringList stringList;
                      QFile textFile("test.txt");
                      if (textFile.open(QIODevice::ReadOnly))
                      {
                      QTextStream textStream(&textFile);
                      while (!textStream.atEnd())
                      {
                      stringList.append(textStream.readLine());
                      cout << "test1";
                      }
                      }
                      foreach(const QString line, stringList)
                      {
                      cout << "test2";
                      cout << line.toStdString();
                      }
                      return 0;
                      }@

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        vsorokin
                        wrote on last edited by
                        #14

                        Are you sure that your test.txt are in same directory that your executable file?

                        --
                        Vasiliy

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mlong
                          wrote on last edited by
                          #15

                          You should print an error message if it cannot open the file.
                          @
                          if (...open...)
                          {
                          ...
                          }
                          else
                          {
                          // tell me about it
                          }
                          @

                          Software Engineer
                          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                          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