Results 1 to 2 of 2

Thread: read from file into array

  1. #1
    Join Date
    Aug 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default read from file into array

    Hi ,
    I have this code which is capable to read from file only one column , I need it read two column. So, how can I alter it to read from file and place the value in array ?
    Note : the data file format like that :

    10.2 30.3 12.4 ........
    34.56 45.9 34.5.......
    12.3 12.45 10.10.....
    .
    .
    .
    Qt Code:
    1. QTextStream in(&file);
    2. unsigned int max_lines = 20000; // we choose 102 because in the real file there are two header lines in the top of file
    3. float data[max_lines];
    4. unsigned int lines_read =0;
    5.  
    6. for(int i=lines_read ;i<max_lines;i++)
    7. {
    8. double x;
    9. QString content;
    10. in >> x;
    11. content = content.setNum(x);
    12.  
    13. data[i]=content.toDouble();
    14. in.readLine();
    15. lines_read++;
    16. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: read from file into array

    Read both of the values you want into variables within your loop body. If you need to skip a value in the middle, read it into a dummy variable.\\

    Why are you using both the input operator (>>) and readline()? Also, why not just read directly into your array instead of performing all the conversions? And since data[] is an array of float, why are you converting to a double?

Similar Threads

  1. declare an array of QSemaphore and array of slot functions
    By radeberger in forum Qt Programming
    Replies: 11
    Last Post: 2nd May 2010, 13:24
  2. How i get text file in array
    By hasnatzaidi in forum Newbie
    Replies: 1
    Last Post: 23rd June 2009, 17:08
  3. Reading values from file to array
    By Jeo_ in forum Newbie
    Replies: 0
    Last Post: 24th April 2009, 18:05
  4. is qt phonon can read realmedia file and divx file
    By fayssalqt in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2009, 15:42
  5. Replies: 1
    Last Post: 20th June 2008, 18:43

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.