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
Forum Update on Monday, May 27th 2025

Problem with reading double values form txt file

Scheduled Pinned Locked Moved Qt Creator and other tools
10 Posts 6 Posters 1.5k Views
  • 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 21 Jan 2020, 23:07 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

    J 1 Reply Last reply 22 Jan 2020, 00:02
    0
    • P Programelllo
      21 Jan 2020, 23:07

      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

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 22 Jan 2020, 00:02 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 22 Jan 2020, 09:31
      3
      • J JKSH
        22 Jan 2020, 00:02

        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 22 Jan 2020, 09:31 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

        J O V 3 Replies Last reply 22 Jan 2020, 10:48
        0
        • P Programelllo
          22 Jan 2020, 09:31

          @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

          J Offline
          J Offline
          JonB
          wrote on 22 Jan 2020, 10:48 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
            22 Jan 2020, 09:31

            @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

            O Offline
            O Offline
            ODБOï
            wrote on 22 Jan 2020, 11:00 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 22 Jan 2020, 12:04
            4
            • P Programelllo
              22 Jan 2020, 09:31

              @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

              V Offline
              V Offline
              VRonin
              wrote on 22 Jan 2020, 11:06 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 22 Jan 2020, 12:42
              5
              • O ODБOï
                22 Jan 2020, 11:00

                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 22 Jan 2020, 12:04 last edited by
                #7

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

                jsulmJ 1 Reply Last reply 22 Jan 2020, 12:08
                0
                • P Programelllo
                  22 Jan 2020, 12:04

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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 22 Jan 2020, 12:08 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 22 Jan 2020, 12:29
                  3
                  • jsulmJ jsulm
                    22 Jan 2020, 12:08

                    @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 22 Jan 2020, 12:29 last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • V VRonin
                      22 Jan 2020, 11:06

                      @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 22 Jan 2020, 12:42 last edited by
                      #10

                      @VRonin your code solved my problem thank you very much

                      1 Reply Last reply
                      0

                      1/10

                      21 Jan 2020, 23:07

                      • Login

                      • Login or register to search.
                      1 out of 10
                      • First post
                        1/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved