Results 1 to 6 of 6

Thread: read a .txt file and store it in a double array

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: read a .txt file and store it in a double array

    Back in C++ I used to do:
    Lets get this straight:
    Qt IS C++ code!
    Just like you don't differentiate between STL or boost and c++, so you don't with Qt.
    Do you understand the concept of a library?

    Should i do read it line by line or store it directly to variable.
    Reading line by line is still just reading.
    Storing in something else - they are not exclusive.

    suppose in Qt there are lots of overloaded functions and classes that let you do this
    What is 'this'?
    Reading from a file?
    Storing your data in an array?
    All of the above?

    And you are right - there are a lot of Classes in Qt.
    but I haven't found them yet
    Here is the full list:
    http://doc.trolltech.com/4.7/classes.html

    It seems you are aware of streams, as you have posted in your code.
    So why not start for example by searching for streams in the Qt documentation?
    If you are aware of streams, you probably are also aware of vectors, which you could also look in the Qt docs.
    Usually the Qt classes have very sensible name, so searching for "file" in the class list will give you some good hits to what you might be looking for, if for example you need some file handling classes.
    Qt has many STL classes reimplemented, take the time to read the documentation.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  2. #2
    Join Date
    Nov 2010
    Posts
    142
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    24

    Default Re: read a .txt file and store it in a double array

    What is 'this'?
    Reading from a file?
    Storing your data in an array?
    All of the above?
    reading the file->taking the actual data that you need->passing the data to a vector/array


    The Documentation is fantastic, but there are tens of classes and functions related to "file". There is no reason to start checking each one of them.Knowledge is not built in this way.
    I started this thread expecting something like:

    for reading a file, try using Qxxxxx
    for obtaining the actual data that you need from the file, try using Qzzzzz
    for passing this data to a vector/array, try using Qrrrrrrr

    Then I would go directly to The Documentation to learn how to use Qxxxxx,Qzzzzz,Qrrrrrrr

  3. #3
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    10
    Thanked 31 Times in 25 Posts

    Default Re: read a .txt file and store it in a double array

    Hi,

    In addition to using the 'old' C++ streams, you have the option to use QFile and QTextStream. Directly from the Qt documentation :

    Qt Code:
    1. QFile file("in.txt");
    2. QTextStream in(&file);
    3. while (!in.atEnd()) {
    4. QString line = in.readLine();
    5. // process your line here
    6. }
    To copy to clipboard, switch view to plain text mode 

    Then, you can use QString::Split to split your line with two numbers into 2 strings, and QString::toDouble() to convert it to a double. The previously mentioned vector and list classes can be used to store the points.

    Best regards,
    Marc

  4. The following 2 users say thank you to marcvanriet for this useful post:

    alimahjoob (22nd September 2013), fatecasino (3rd December 2010)

Similar Threads

  1. how to import a file into qt when double clicking a file.
    By berlinud in forum Qt Programming
    Replies: 14
    Last Post: 16th September 2010, 16:13
  2. read from file into array
    By obad in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2010, 08:26
  3. efficient way to store menu items in file
    By h123 in forum Qt Programming
    Replies: 5
    Last Post: 24th July 2009, 06:52
  4. Replies: 1
    Last Post: 17th July 2009, 08:22
  5. Replies: 12
    Last Post: 17th June 2009, 05:34

Tags for this Thread

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.