Results 1 to 20 of 20

Thread: How to read a value from tow columns file !

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2015
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    9

    Default How to read a value from tow columns file !

    Hallo Guys,

    I have a txt-File, which contains values like that :

    distance force
    0.090721 0.0072147
    0.181527 0.0139748
    0.270888 0.0203682
    0.360796 0.0262649
    0.451177 0.0317043
    0.541753 0.0366577
    0.632632 0.0409662
    0.724928 0.0451153

    I just need to read the tow values of the last line but separately. For example: if i pressed a Qpushbutton (1) i get 0.724928, and if i pressed a Qpushbutton (2) i get 0.0451153 .

    i've tried the following code, but i always get the first value of the last line (0.724928)..my question is how to get the second value of the last line (0.0451153) ?

    QFile inputFile("measurnments.txt");
    if (inputFile.open(QIODevice::ReadOnly))
    {
    QTextStream in(&inputFile);
    while (!in.atEnd())
    {
    QString line = in.readLine();
    this->display->setText(line);
    }
    inputFile.close();
    }

    I'm just a Newbie , so i'll be grateful und i'll appreciate it if somone could help me


    Best Reagrds
    Last edited by mejacky; 4th October 2015 at 14:31.

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 14 Times in 14 Posts

    Default Re: How to read a value from tow columns file !

    readLine() reads complete line, you need to split that line based on space.

    QStringList list = line.split(" ");
    Now list.at(0) contains 1st value
    list.at(1) contains 2nd value
    Last edited by prasad_N; 4th October 2015 at 16:25. Reason: Typo
    Thanks :-)

  3. The following user says thank you to prasad_N for this useful post:

    mejacky (6th October 2015)

  4. #3
    Join Date
    Oct 2015
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    9

    Default Re: How to read a value from tow columns file !

    first of all thx a lot for the quick reply,

    i've tried the QStringList list = line.split(" "), but when i leave a space between " " i get the following Debug Error:


    Unbenannt.pngUnbenannt.pngUnbenannt.png

    when i write QStringList list = line.split("") without a space, and then write for example list.at(2), i get (.) which is the comma in 0.724928 and when i write for example list.at(5), i get 4 which is the fifth chracter in 0.724928 .

    can you tell me please, where i'm going wrong

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: How to read a value from tow columns file !

    list.at(2) is not a valid, your list only contains two elements: the number before the space and the number after the space.
    So the only valid indexes are 0 and 1

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    mejacky (6th October 2015)

  7. #5
    Join Date
    Oct 2015
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    9

    Default Re: How to read a value from tow columns file !

    hey..but as i wrote when i write list.at(0) i get just the first character (0) of the numer 0.724928 and when i write list.at(1) i get (.)

    do you have any idea why is that happening ?

    Regards

  8. #6
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 14 Times in 14 Posts

    Default Re: How to read a value from tow columns file !

    This should not happend unless you have spces between numbers or else you might doing line.at(0) or line.at(1).
    show your code.
    Thanks :-)

  9. The following user says thank you to prasad_N for this useful post:

    mejacky (6th October 2015)

  10. #7
    Join Date
    Oct 2015
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    9

    Default Re: How to read a value from tow columns file !

    i think every chracter is being interpreted as a column or an element und not the whole number 0.724928.. so if i give list.at(15) i get 1 from the other number 0.0451153..

    so how could i solve the problem ?

  11. #8
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 14 Times in 14 Posts

    Default Re: How to read a value from tow columns file !

    Show your splitting part of the code plz
    Thanks :-)

  12. The following user says thank you to prasad_N for this useful post:

    mejacky (6th October 2015)

  13. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: How to read a value from tow columns file !

    Quote Originally Posted by mejacky View Post
    hey..but as i wrote when i write list.at(0) i get just the first character (0) of the numer 0.724928 and when i write list.at(1) i get (.)
    And as you wrote you get that when you are not using the space separator to split on.

    So prasad_N made a typo in an otherwise good answer. As a programmer you could have looked at the error, then figured that one of your two list.at() calls must be the problem, then checked which one and then fixed it after consulting the documentation.

    But instead you randomly changed something else, getting a totally different and for your purpose unusable result.

    One of the most important skills for a developer is to anaylize error situations.
    Asking for help for every minor problem is not viable in the long run.

    Cheers,
    _

  14. The following 2 users say thank you to anda_skoa for this useful post:

    mejacky (6th October 2015), prasad_N (4th October 2015)

  15. #10
    Join Date
    Oct 2015
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    9

    Default Re: How to read a value from tow columns file !

    Thank you a lot guys for your replys..i'm still getting the same Probleme, but I will keep trying until it works


    Best Regards

  16. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: How to read a value from tow columns file !

    You have been given the solution for the by-character, rather than by-column, split. If you have done that change, that is replaced
    Qt Code:
    1. QStringList list = line.split("")
    2. // with
    3. QStringList list = line.split(" ")
    To copy to clipboard, switch view to plain text mode 
    , and the character between the columns is indeed a space (not a Tab) then you must be seeing a different problem. You are not to describing the new problem, and doing so would definitely help.

  17. The following user says thank you to ChrisW67 for this useful post:

    mejacky (6th October 2015)

Similar Threads

  1. Read contents from the file using QHTTP Read()?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 09:03
  2. Make a column read-only in a QSqlTableModel/QTableView
    By graciano in forum Qt Programming
    Replies: 0
    Last Post: 15th November 2009, 19:18
  3. read ascii files with different column counts
    By pospiech in forum Qt Programming
    Replies: 5
    Last Post: 6th November 2009, 13:42
  4. Replies: 6
    Last Post: 6th August 2009, 18:18
  5. Replies: 8
    Last Post: 28th May 2007, 21:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.