Results 1 to 6 of 6

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

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

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

    Hi there,
    supposing that i a have a data.txt with two columns of doubles,

    23 4.43
    1.2 232
    3.444 23.23
    ......... (we don't know the data size until we open the file)

    and I want to store it (dynamically) in a 2d array.
    How can i do it in Qt? I read several posts in the forum and I got really confused!

    PS:what's the simplest way to print out the array to confirm that the data was read correctly? in c++ it was simply cout, in Qt?

  2. #2
    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: read a .txt file and store it in a double array

    You could use a QVector<QVector<double, double> >, QVector<QVector2D>, or QVector<PointF> depending on what makes most sense. You could also use QList<> as the outer container. QPair<double, double> may also be useful.

    Using Qt does not change anything regarding the features of C++. You may use std::cout for for debugging duties, or you can use qDebug() and related functions from Qt.

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

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

    hi ChrisW67,

    thanks for your answer. Supposingly this problem was simple in C++, but now I have spent around 12 hours of searching and still haven't got it!
    Hence:
    Part A:I should read in the data from file
    Should i do read it line by line or store it directly to variable. Back in C++ I used to do:
    Qt Code:
    1. ifstream in(FileName_Read);
    2. while(getline(in,line,'\n')) {
    3.  
    4. // Declarations.
    5. stringstream ss;
    6. ss << line;
    7. string entry;
    8.  
    9. while (getline(ss,entry,delimiter)) {
    10.  
    11. // Declarations.
    12. stringstream entry_ss;
    13. float entry_float;
    14. char entry_char;
    15.  
    16. // Insert data entry into stringstream.
    17. entry_ss << entry;
    18.  
    19. // Convert data entry to float.
    20. entry_ss >> entry_float;
    To copy to clipboard, switch view to plain text mode 
    I suppose in Qt there are lots of overloaded functions and classes that let you do this in 3-4 lines, but I haven't found them yet
    Part B: Store the data to a local variable
    So far in Qt I was using C++ double pointers,I hadn't discovered QVector. I will check it out now.

    PS:isn't it strange that when you google Qt problems you are almost never sent to qtcentre forums?? Actually, you have to use qtcentre search machine to find what you need and that takes a lot of time.

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

    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.

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

    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

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

    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

  7. 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.