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. [SOLVED] QTextStream to read a file
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QTextStream to read a file

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 3.5k 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.
  • P Offline
    P Offline
    PabloAG
    wrote on last edited by
    #1

    Hi!

    I want to read a .txt file using Qt. The file has this content:

    height: 184.2
    lenght: 21.4
    color: 47
    ...

    I only want to store these values (184.2, 21.4, 47) in my variables. By now, I'm reading the file line by line and I'm "converting" those strings into float numbers. This is what I'm doing:

    @myString = myStream.readLine();
    myString.remove(QRegExp("[abcdefghijklmnopqrstuvwxyz_: ]"));
    myFloat = myString.toFloat();@

    The problem comes when I try to read a number in scientific notation (for example 5e-6). My code doesn't work because it removes the letter "e". ¿How can I solve this? I'm sure there's a smarter way of managing all this.

    Thanks in advance

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi, and welcome to the Qt Dev Net!

      If every line has the same format, call
      @
      myString.split(": ");
      @

      This will, for example, split your first line into a list of 2 strings: "height" and "184.2". Then, you can discard the first string and keep the second.

      You can find many more useful functions in the "QString documentation":http://qt-project.org/doc/qt-5/QString.html.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Soraltan
        wrote on last edited by
        #3

        I.e. it states specifically that the 5e-6 format is "supported":http://qt-project.org/doc/qt-4.8/qstring.html#toDouble

        So as JKSH maintains the 'e' in the notation, this should be a working solution.

        Soraltan

        P.S.: If you should have problems with leading or tailing white spaces use "QString::trimmed()":http://qt-project.org/doc/qt-4.8/qstring.html#trimmed

        1 Reply Last reply
        0
        • IamSumitI Offline
          IamSumitI Offline
          IamSumit
          wrote on last edited by
          #4

          Hii try this,

          QFile mFile("d:/a.txt");

          mFile.open(QIODevice::Text|QIODevice::ReadOnly);

          QTextStream myStream(&mFile);

          while(!myStream.atEnd())

          {

          QString myString = myStream.readLine();

          //qDebug()<<myString;

          myString=myString.mid(myString.lastIndexOf(":")+2,myString.length());

          //qDebug()<<"222 : " << myString;

          double myDbl = myString.toDouble();

          qDebug()<<myDbl;

          }

          a.close();

          Be Cute

          1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            [quote author="IamSumit" date="1400063233"]
            myString=myString.mid(myString.lastIndexOf(":")+2,myString.length());[/quote]Why so complicated? That's what QString::split() is for

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • P Offline
              P Offline
              PabloAG
              wrote on last edited by
              #6

              Thanks! Split worked perfectly.

              1 Reply Last reply
              0
              • IamSumitI Offline
                IamSumitI Offline
                IamSumit
                wrote on last edited by
                #7

                [quote author="JKSH" date="1400065270"][quote author="IamSumit" date="1400063233"]
                myString=myString.mid(myString.lastIndexOf(":")+2,myString.length());[/quote]Why so complicated? That's what QString::split() is for[/quote]

                Yeah You are right ..but in some cases it will be failed .

                Like to obtain Ratio on a file
                Length : width : 1:2
                isn't it?

                Be Cute

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Soraltan
                  wrote on last edited by
                  #8

                  Like always when doing some kind of parsing you can only provide a working solution for a given set of 'language constraints'. Such as "Does only one ':' occur in a line?" JKSH's solution assumes that.

                  I would understand your example in a way, that '1:2' should be returned, while yours solution above would return only the 2, wouldn't it?

                  Anyway, @PabloAG if considers your problem to be solved, please mark the thread by prepending a [Solved] in the title.

                  Best

                  Soraltan

                  1 Reply Last reply
                  0
                  • JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    [quote author="IamSumit" date="1400134234"]Yeah You are right ..but in some cases it will be failed .

                    Like to obtain Ratio on a file
                    Length : width : 1:2
                    isn't it?
                    [/quote]That's why I said "If every line has the same format". :) PabloAG said he has floating point numbers, not ratios.

                    [quote author="PabloAG" date="1400109977"]Thanks! Split worked perfectly.[/quote]You're welcome. Happy coding!

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    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