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. Get specific point from a .txt file
Forum Updated to NodeBB v4.3 + New Features

Get specific point from a .txt file

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 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.
  • KonstantinosK Offline
    KonstantinosK Offline
    Konstantinos
    wrote on last edited by
    #1

    Imagine that a .txt file is like this:

    ...
    Line 8: 1 - 2 yes 0.002452
    Line 9: 1 - 3 no 0.015837
    Line 10: 1 - 4 yeah 0.026817
    ...

    And I want to take the number 0.015837 in line 9. Is there something like get(9,5)?

    get(9,5) = "0.015837"
    get(10,2) = "-"
    get(8,1) = "1"
    get(9,4) = "no"

    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Not really :)
      a text files is a stream of bytes so its hard to
      navigate to a line by random access as you dont really know
      where lines starts or end unless u read and keep track.

      Can I ask what this file is ?

      Is it too big to read into memory ?

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        You can read in the file and look at each line this way

        QFile file("c:/myfolder/myfile.txt");
        if(!file.open(QIODevice::ReadOnly)) {
            QMessageBox::information(0, "error", file.errorString());
        }
        
        QTextStream in(&file);
        // format is:
        //1 - 2 yes 0.002452
        while(!in.atEnd()) {
            QString line = in.readLine();    // read one line
            QStringList fields = line.split(" ");    // split it to a list at all spaces
            // fields[0] is 1
            // fields[2] is -
            // fields[3] is 2
            // fields[4] is yes 
            // fields[5] is 0.002452 
        }
        file.close();
        
        1 Reply Last reply
        0
        • KonstantinosK Offline
          KonstantinosK Offline
          Konstantinos
          wrote on last edited by Konstantinos
          #4

          It is a file that is generated after a calibration.

          See the image, from notepad++: http://s3.postimg.org/6b35e23er/calibration.png

          But why I don't really know where the lines start or end? It is not so difficult to be seen by human eye.

          All .txt files have lines, and you can distinguish the elements of any line, just by a space between them.

          So, it is really so difficult to get the 3rd element of the 8th line of a .txt file?

          edit: @mrjj, thanks, but I think in your case, fields[1] is blank? And I can take the number 0.002452 directly or step by step and digit by digit?

          mrjjM kshegunovK 2 Replies Last reply
          1
          • KonstantinosK Konstantinos

            It is a file that is generated after a calibration.

            See the image, from notepad++: http://s3.postimg.org/6b35e23er/calibration.png

            But why I don't really know where the lines start or end? It is not so difficult to be seen by human eye.

            All .txt files have lines, and you can distinguish the elements of any line, just by a space between them.

            So, it is really so difficult to get the 3rd element of the 8th line of a .txt file?

            edit: @mrjj, thanks, but I think in your case, fields[1] is blank? And I can take the number 0.002452 directly or step by step and digit by digit?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            well its not how files works. :)
            But see code, its not so difficult to read in.
            If all have spaces U can easy split into a list.

            mrjjM 1 Reply Last reply
            0
            • mrjjM mrjj

              well its not how files works. :)
              But see code, its not so difficult to read in.
              If all have spaces U can easy split into a list.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @Konstantinos said:
              but I think in your case, fields[1] is blank? And I can take the number 0.002452 directly or step by step and digit by digit?

              Whoops. its a typo. there be [1] also
              // fields[0] is 1
              // fields[1] is -
              // fields[2] is 2
              // fields[3] is yes
              // fields[4] is 0.002452
              It will split at space.

              You will get "0.002452" from the list[4]
              (full number)

              EDIT:
              and u should skip first line.

              1 Reply Last reply
              0
              • KonstantinosK Offline
                KonstantinosK Offline
                Konstantinos
                wrote on last edited by
                #7

                Ok thanks @mrjj. I wanted exactly this...

                mrjjM 1 Reply Last reply
                1
                • KonstantinosK Konstantinos

                  Ok thanks @mrjj. I wanted exactly this...

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Konstantinos
                  Super :)
                  For a little error check, u can check fields.size()
                  and see if it has expected number of entries.
                  That way u can skip lines if not valid.

                  1 Reply Last reply
                  0
                  • KonstantinosK Konstantinos

                    It is a file that is generated after a calibration.

                    See the image, from notepad++: http://s3.postimg.org/6b35e23er/calibration.png

                    But why I don't really know where the lines start or end? It is not so difficult to be seen by human eye.

                    All .txt files have lines, and you can distinguish the elements of any line, just by a space between them.

                    So, it is really so difficult to get the 3rd element of the 8th line of a .txt file?

                    edit: @mrjj, thanks, but I think in your case, fields[1] is blank? And I can take the number 0.002452 directly or step by step and digit by digit?

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by kshegunov
                    #9

                    @Konstantinos

                    But why I don't really know where the lines start or end? It is not so difficult to be seen by human eye.
                    All .txt files have lines, and you can distinguish the elements of any line, just by a space between them.

                    Three reasons:

                    1. Line endings. The special characters that mark where a line ends are different for different platforms.
                      • Linux uses the line feed character "\n"
                      • Windows uses carriage return followed by line feed "\r\n"
                      • And finally Mac uses carriage return only "\r"
                    2. Text encoding. Characters are represented as numbers so a given number would correspond to a given character, hence you can see them on the screen. Now, consider the amount of languages and characters that people had invented during the ages ... So the encoding specifies which character is represented by what number, but because just having one big table for all languages is pretty inefficient there are the encoding schemes that use variable length to represent a character (like UTF-8, one of the most prominent examples). This means that a character may be represented by 1, 2, 3 or even 4 consecutive 1 byte numbers, so you can't tell before actually reading the file.
                    3. Line length. You can't know how long a line of text is before actually reading it.

                    Kind regards.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    1

                    • Login

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