Results 1 to 17 of 17

Thread: How to read numbers from a File and store in vector

  1. #1
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to read numbers from a File and store in vector

    Hello,

    I wish to know how to read numbers from a file and store them in something like a vector.

    My file is a .txt with numbers in rows and columns:

    12 34 567 423 788
    23 343 45 56 67 78
    ...

    I've already been able to read the file using:

    Qt Code:
    1. QFile myFile(file);
    2.  
    3. if(!myFile.open(QFile::ReadOnly))
    4. {
    5. qDebug() << "Cannot open...";
    6. return;
    7. }
    8.  
    9. QTextStream input(&myFile);
    10. QString line;
    11.  
    12. line = input.readLine();
    13.  
    14. qDebug() << line;
    To copy to clipboard, switch view to plain text mode 

    It displays correctly the line of my file.
    Now, I need to store each numerical value of the file to a vector or similar, so I can do operations over those numbers.
    Something like:

    Qt Code:
    1. int VectorFirstLine[5];
    2.  
    3. qDebug() << VectorFirstLine[3];
    4.  
    5. VectorFirstLine[5]*AnotherVariable;
    To copy to clipboard, switch view to plain text mode 


    And so on... What is the best way to do that? Using a QtList? a QVector?

    Thank you very much!

  2. #2
    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: How to read numbers from a File and store in vector

    Here are some hints:
    - You can split strings using QString::split()
    - You can covert strings to ints using QString::toInt()
    ==========================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.

  3. #3
    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 numbers from a File and store in vector

    The QList page discusses the different containers.

    Another hint:
    You can create two-dimensional arrays of items using a list or lists or vector of vectors approach.

  4. #4
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    2

    Default Re: How to read numbers from a File and store in vector

    What is the Qt equivalent of the following C++ procedure of reading a column of numbers from a file and storing them to a vector?



    Qt Code:
    1. double *Hydraulics::elevationy(int im) {
    2.  
    3. ifstream bin("elevy.in");
    4.  
    5. double *hpi = new double [im+1];
    6.  
    7. for(int i=1; i<=im; i++)
    8. bin>>hpi[i];
    9.  
    10. bin.close();
    11. return hpi;
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    where elevy.in is the input file, of course
    Last edited by high_flyer; 18th February 2011 at 11:23. Reason: code tags

  5. #5
    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: How to read numbers from a File and store in vector

    Have a look at QDataStream and QVector and try to figure it out.
    Ask again if you get stuck.
    ==========================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.

  6. #6
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    2

    Default Re: How to read numbers from a File and store in vector

    I tried to but failed...

    Would you please write the Qt equivalent please?

    thank you in advance

  7. #7
    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: How to read numbers from a File and store in vector

    I tried to but failed...
    Show your code.
    ==========================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.

  8. #8
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to read numbers from a File and store in vector

    Quote Originally Posted by bramadani View Post
    What is the Qt equivalent of the following C++ procedure of reading a column of numbers from a file and storing them to a vector?



    Qt Code:
    1. double *Hydraulics::elevationy(int im) {
    2.  
    3. ifstream bin("elevy.in");
    4.  
    5. double *hpi = new double [im+1];
    6.  
    7. for(int i=1; i<=im; i++)
    8. bin>>hpi[i];
    9.  
    10. bin.close();
    11. return hpi;
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    where elevy.in is the input file, of course
    In the general case, this code will not read a column of numbers from a file. It will only handle the degenerate case where the files contains a single column of numbers. Even then, it won't work unless the count passed exactly matches the number of numbers in the file.

    From a C/C++ perspective, there are also indexing issues; array indexing normaly starts with zero.

  9. #9
    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: How to read numbers from a File and store in vector

    Look, if you want help you can at least TRY to help us help you.
    Your original question was:
    What is the Qt equivalent of the following C++ procedure of reading a column of numbers from a file and storing them to a vector?
    I then suggested to you two Qt classes to be used for converting the code you posted to its Qt equivalent.
    On which you answered you have tried it, but it didn't work.
    However the code you reposted is your old code which is NOT using Qt classes - so what have you tried?

    In the general case, this code will not read a column of numbers from a file. It will only handle the degenerate case where the files contains a single column of numbers. Even then, it won't work unless the count passed exactly matches the number of numbers in the file.

    From a C/C++ perspective, there are also indexing issues; array indexing normaly starts with zero.
    This is another issue.
    Lets do it one step at a time.
    ==========================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.

  10. #10
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    2

    Default Re: How to read numbers from a File and store in vector

    Ok, so here is the code which I tried using Qt classes

    Qt Code:
    1. double *Hydraulics::elevationx(int im) {
    2.  
    3. double xpi[im+1];
    4. QFile bin("elevx.in");
    5. QTextStream input(&bin);
    6. QString line;
    7. line = input.readLine().split();
    8. xpi = line.toDouble();
    9.  
    10. return xpi;
    11. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    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: How to read numbers from a File and store in vector

    This code can't possibly compile because split must take at least one parameter, and it returns a QStringList and not a QString.

    It seems you have no interest in solving this your self.
    In this forum we like to help as much as we can, but don't expect us to do you work and thinking for you.

    Once you have a real problem which doesn't boils down to "do this for me", you are welcome to try again.
    ==========================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.

  12. #12
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to read numbers from a File and store in vector

    Quote Originally Posted by high_flyer View Post
    This is another issue.
    Lets do it one step at a time.
    Step 0: Make a clear, precise statement of the problem to be solved. :-)

    Here, it isn't at all clear what sort of file is to be read; there seems to be an assumption, but I've found through bitter experience that assumptions are often wrong.

  13. #13
    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 numbers from a File and store in vector

    Read the comment I have placed in your code:
    Quote Originally Posted by bramadani View Post
    Ok, so here is the code which I tried using Qt classes

    Qt Code:
    1. double *Hydraulics::elevationx(int im) {
    2.  
    3. double xpi[im+1];
    4. // You want storage for im doubles but allocated storage for one more than that
    5.  
    6. QFile bin("elevx.in");
    7. // excellent, but you need to do something to open the file for reading
    8.  
    9. QTextStream input(&bin);
    10. // fine, at least if the file is open
    11.  
    12. QString line; // ok
    13. line = input.readLine().split();
    14. // QString::split() needs and argument and does not return QString
    15. // Your original C++ code reads each line as a single number, you don't need split() to do that here.
    16.  
    17. xpi = line.toDouble();
    18. // Do you care if the conversion fails?
    19. // xpi is a pointer to an array of doubles, not a double.
    20.  
    21. // Did you want to do this only for the first line of the file, or for every line in the file?
    22. // Your original C++ had a loop construct
    23. // What happens if the file is longer or shorter than "im" elements?
    24.  
    25. return xpi;
    26. // The storage for xpi[] is deallocated at the end of this function... this is bad for the receiving code which gets a pointer to a block of random memory
    27. }
    To copy to clipboard, switch view to plain text mode 
    You should look at std::vector or QVector as better methods of storing arrays of values.
    Last edited by ChrisW67; 20th February 2011 at 22:09.

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

    bramadani (21st February 2011)

  15. #14
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    2

    Default Re: How to read numbers from a File and store in vector

    Thanks for the comments and the help

    I tried the following code and it managed to compile, hope it does what I need


    Qt Code:
    1. double *Hydraulics::elevationx(int im) {
    2.  
    3. double *xpi = new double[im+1];
    4. QFile bin("elevx.in");
    5. QTextStream input(&bin);
    6. QString line;
    7. int i = 1;
    8.  
    9. while(!input.atEnd()) {
    10. line = input.readLine();
    11. xpi[i] = line.toDouble();
    12. i++;
    13. }
    14. return xpi;
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 21st February 2011 at 16:06. Reason: code tags

  16. #15
    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: How to read numbers from a File and store in vector

    This code will work only if it is guaranteed that the file format is such, that each line contains nothing more than a numeric value and a line break.
    You have completly ignore many important comments the previous poster had put in your code - such as opening the file and many others.
    Also its is recommended to use container classes instead of C type arrays, such as QVector or QList.
    ==========================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.

  17. #16
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    2

    Default Re: How to read numbers from a File and store in vector

    Yes, so far the files are guaranteed to be in a coulmn format line

    234
    145532
    123
    35245
    342
    76
    7546
    .
    .
    324

    and so on

    However I am concerned what if the files are separated with comma values but not in the same distance

    e.g.

    0 , 54
    15 , 65
    20 , 43
    25 ,64
    30,100

    I know that for these cases we use the split(); function however if we say take the argument as split(","); then the strings will include the empty spaces as well, right? Is there any way out of this?

    Thank you!

  18. #17
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to read numbers from a File and store in vector

    You can use QString::trimmed to remove white spaces from the start and the end of a string.

  19. The following user says thank you to stampede for this useful post:

    bramadani (22nd February 2011)

Similar Threads

  1. read a .txt file and store it in a double array
    By fatecasino in forum Newbie
    Replies: 5
    Last Post: 3rd December 2010, 20:13
  2. efficient way to store menu items in file
    By h123 in forum Qt Programming
    Replies: 5
    Last Post: 24th July 2009, 06:52
  3. Replies: 1
    Last Post: 17th July 2009, 08:22
  4. Replies: 12
    Last Post: 17th June 2009, 05:34
  5. How to read/write sets of orderded numbers in binary
    By kaydknight in forum General Programming
    Replies: 3
    Last Post: 12th March 2007, 05:50

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.