Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Problem with reading double values form txt file

Problem with reading double values form txt file

Scheduled Pinned Locked Moved Qt Creator and other tools
10 Posts 6 Posters 1.8k Views 2 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
    Programelllo
    wrote on last edited by
    #1

    I have .txt file that looks something like this:
    1.3472
    0.4883
    1
    ....
    3.4883

    I need to store this values in array, vector or something else but I can't find the way how to do it?
    If someone can help me out here it would help me a lot.
    Thanks

    JKSHJ 1 Reply Last reply
    0
    • P Programelllo

      I have .txt file that looks something like this:
      1.3472
      0.4883
      1
      ....
      3.4883

      I need to store this values in array, vector or something else but I can't find the way how to do it?
      If someone can help me out here it would help me a lot.
      Thanks

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi, and welcome!

      @Programelllo said in Problem with reading double values form txt file:

      I can't find the way how to do it?

      What programming experience do you have? Who gave you this task?

      If this is for a course or class, what do your notes suggest?

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

      P 1 Reply Last reply
      3
      • JKSHJ JKSH

        Hi, and welcome!

        @Programelllo said in Problem with reading double values form txt file:

        I can't find the way how to do it?

        What programming experience do you have? Who gave you this task?

        If this is for a course or class, what do your notes suggest?

        P Offline
        P Offline
        Programelllo
        wrote on last edited by
        #3

        @JKSH I'm working on ui that converts money currencies
        And because of it coefficient variability i stored coefficient in xlsx file that i converted in txt file so i need now to read that coefficients. It's project for faculty, and i haven't got any help

        JonBJ ODБOïO VRoninV 3 Replies Last reply
        0
        • P Programelllo

          @JKSH I'm working on ui that converts money currencies
          And because of it coefficient variability i stored coefficient in xlsx file that i converted in txt file so i need now to read that coefficients. It's project for faculty, and i haven't got any help

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @Programelllo
          I don't know what "coefficient" is about. But all you have to do is pick whichever C++ library call you wish to use to read the lines from file and convert to double numbers (e.g. https://doc.qt.io/qt-5/qtextstream.html#readLine + https://doc.qt.io/qt-5/qstring.html#toDouble, or https://doc.qt.io/qt-5/qtextstream.html#operator-gt-gt-11), and then store them in, say, QVector<double>. Which bit is a problem?

          1 Reply Last reply
          2
          • P Programelllo

            @JKSH I'm working on ui that converts money currencies
            And because of it coefficient variability i stored coefficient in xlsx file that i converted in txt file so i need now to read that coefficients. It's project for faculty, and i haven't got any help

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #5

            hi
            @Programelllo said in Problem with reading double values form txt file:

            And because of it coefficient variability i stored coefficient in xlsx file

            Because they vary very often you can get them directly from a webservice.

            For reading a text file line by line you can use QFile class

            QFile txtFile(fileName);
            if (txtFile.open(QIODevice::ReadOnly))
            {
               QTextStream in(&txtFile);
               while (!in.atEnd())
               {
                  QString line = in.readLine();
                  ...
               }
               txtFile.close();
            }
            
            P 1 Reply Last reply
            4
            • P Programelllo

              @JKSH I'm working on ui that converts money currencies
              And because of it coefficient variability i stored coefficient in xlsx file that i converted in txt file so i need now to read that coefficients. It's project for faculty, and i haven't got any help

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              @Programelllo said in Problem with reading double values form txt file:

              stored coefficient in xlsx file that i converted in txt file

              Qt can read xlsx directly using the ODBC SQL Driver: https://wiki.qt.io/Handling_Microsoft_Excel_file_format#Using_ODBC

              Small tweak to @LeLev program:

              QVector<double> values;
              QFile txtFile(fileName);
              if (txtFile.open(QIODevice::ReadOnly | QIODevice::Text))
              {
                  QTextStream in(&txtFile);
                  QString line;
                  QLocale locale; // this determines decimal and thousand separators
                  double value=0.0;
                  bool ok=false;
                  while (stream.readLineInto(&line)) {
                      value=locale.toDouble(line,&ok);
                      if(ok) values << value;
                  }
              }
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              P 1 Reply Last reply
              5
              • ODБOïO ODБOï

                hi
                @Programelllo said in Problem with reading double values form txt file:

                And because of it coefficient variability i stored coefficient in xlsx file

                Because they vary very often you can get them directly from a webservice.

                For reading a text file line by line you can use QFile class

                QFile txtFile(fileName);
                if (txtFile.open(QIODevice::ReadOnly))
                {
                   QTextStream in(&txtFile);
                   while (!in.atEnd())
                   {
                      QString line = in.readLine();
                      ...
                   }
                   txtFile.close();
                }
                
                P Offline
                P Offline
                Programelllo
                wrote on last edited by
                #7

                @LeLev i just need to know how this line convert to double?

                jsulmJ 1 Reply Last reply
                0
                • P Programelllo

                  @LeLev i just need to know how this line convert to double?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #8

                  @Programelllo Well, you can read the documentation: https://doc.qt.io/qt-5/qstring.html#toDouble
                  Or read what @VRonin wrote.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  P 1 Reply Last reply
                  3
                  • jsulmJ jsulm

                    @Programelllo Well, you can read the documentation: https://doc.qt.io/qt-5/qstring.html#toDouble
                    Or read what @VRonin wrote.

                    P Offline
                    P Offline
                    Programelllo
                    wrote on last edited by
                    #9

                    @jsulm how than i access to certain member of vector values?

                    1 Reply Last reply
                    0
                    • VRoninV VRonin

                      @Programelllo said in Problem with reading double values form txt file:

                      stored coefficient in xlsx file that i converted in txt file

                      Qt can read xlsx directly using the ODBC SQL Driver: https://wiki.qt.io/Handling_Microsoft_Excel_file_format#Using_ODBC

                      Small tweak to @LeLev program:

                      QVector<double> values;
                      QFile txtFile(fileName);
                      if (txtFile.open(QIODevice::ReadOnly | QIODevice::Text))
                      {
                          QTextStream in(&txtFile);
                          QString line;
                          QLocale locale; // this determines decimal and thousand separators
                          double value=0.0;
                          bool ok=false;
                          while (stream.readLineInto(&line)) {
                              value=locale.toDouble(line,&ok);
                              if(ok) values << value;
                          }
                      }
                      
                      P Offline
                      P Offline
                      Programelllo
                      wrote on last edited by
                      #10

                      @VRonin your code solved my problem thank you very much

                      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