Results 1 to 20 of 20

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

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

    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 13:31.

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

    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 15: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
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    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
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    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
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    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
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    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
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    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
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    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
    Oct 2015
    Posts
    9
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

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

    i'm sorry but which code do you mean ?

    how i saved the values or how iam i reading them ?

    File inputFile("measurnments.txt");
    if (inputFile.open(QIODevice::ReadOnly))
    {
    QTextStream in(&inputFile);
    while (!in.atEnd())
    {
    QString line = in.readLine();
    QStringList list = line.split("");
    QString val = list.at(1);
    this->display->setText(val);
    }
    inputFile.close();
    }

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

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

    QStringList list = line.split(" ");
    list.at(0)
    list.at(1)
    Will surly work man.
    //show this part if your doing in diff way
    Thanks :-)

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

    mejacky (6th October 2015)

  16. #11
    Join Date
    Oct 2015
    Posts
    9
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

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

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

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

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

    line.split(" "); will do.
    If you still have problem print & check what you are reading into line before split it.
    Thanks :-)

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

    mejacky (6th October 2015)

  19. #13
    Join Date
    Oct 2015
    Posts
    9
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

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

    i'm still having the same problem..i'm sorry but what do you mean by print & check into line ?

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

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

    After
    Qstring line = in.readLine();
    qDebug() << line; //see what your are reading into line
    Thanks :-)

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

    mejacky (6th October 2015)

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

    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,
    _

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

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

  24. #16
    Join Date
    Oct 2015
    Posts
    9
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    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

  25. #17
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    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.

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

    mejacky (6th October 2015)

  27. #18
    Join Date
    Oct 2015
    Posts
    9
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

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

    hey Chris...i've tried everything actually, but whenever i leave a space or tab between the "", i become this failure ( QList index out of range )..i really don't know how this could be solved !

    Error:"ASSERT failure in QList::at: "index out of range", file ../../../../Qt/2010.05/qt/include/QtCore/../../src/corelib/tools/qlist.h, line 455 Invalid parameter passed to C runtime function. Invalid parameter passed to C runtime function."


    Added after 29 minutes:


    wow Chris thx a lot...actually it was a tab and not a space , cause according to the way how i saved the values i always put a tab between them...now it works perfect

    i've noticed that with help from qdebug() ..thx prasad_N

    i really appericiate all your replys

    Best reagrds from germany
    Last edited by mejacky; 6th October 2015 at 12:28.

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

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

    You can also use the regular expression variant of the split() function to split on any whitespace:
    Qt Code:
    1. str = "Some text\t\twith strange whitespace.";
    2. list = str.split(QRegExp("\\s+"));
    To copy to clipboard, switch view to plain text mode 

  29. #20
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

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

    Quote Originally Posted by mejacky View Post
    i've noticed that with help from qdebug() ..thx prasad_N
    I hope you don't mind me posting, but you really should spend some time and learn how to set breakpoints and inspect variables using the Qt Creator debugger. This could have been solved very quickly had you simply set a breakpoint after you split the line and inspect your QStringList. You would have noticed that the line was actually not split and consisted of a single value and the QStringList count would have been 1.

    Once you become good at debugging, you'll save tons of time solving trivial problems. You'll never solve more complex problems with the shotgun approach in my experience.

    Good luck!
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Read contents from the file using QHTTP Read()?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 08:03
  2. Make a column read-only in a QSqlTableModel/QTableView
    By graciano in forum Qt Programming
    Replies: 0
    Last Post: 15th November 2009, 18:18
  3. read ascii files with different column counts
    By pospiech in forum Qt Programming
    Replies: 5
    Last Post: 6th November 2009, 12:42
  4. Replies: 6
    Last Post: 6th August 2009, 17:18
  5. Replies: 8
    Last Post: 28th May 2007, 20: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.