Results 1 to 9 of 9

Thread: Data Reading and Storing in arrays

  1. #1
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Data Reading and Storing in arrays

    Hi,

    i'm new to both qt and c++, so please pardon me if i'm asking something basic.

    i need to open and read a txt file with the following data as example

    1|20|30|25
    2|20|30|25
    3|20|30|25
    4|20|30|25
    5|20|30|25

    the first column is the n-th repetition while the 2nd, 3rd, 4th is the x, y and z coordinate respectively.

    i will like to store them in arrays so that i can take the data out again for processing later on. i think it can be done by two while loops, with the first reading line by line and the second within the first and reading the individual value separated by the vertical bar separator. but i'm not really sure how it can be done. can anyone help me by providing some examples or guidance?

    Thanks for any help in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Data Reading and Storing in arrays

    It's much simpler than that.

    Qt Code:
    1. QFile f(...);
    2. f.open(...);
    3. QList<QVector3D> points;
    4. while(!f.atEnd()) {
    5. QString line = f.readLine();
    6. QStringList list = line.split('|');
    7. QVector3D vec;
    8. vec.setX(list.at(1).toInt());
    9. vec.setY(list.at(2).toInt());
    10. vec.setZ(list.at(3).toInt());
    11. points << vec;
    12. }
    To copy to clipboard, switch view to plain text mode 

    Of course you need to provide some error checking.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    kango (4th January 2013)

  4. #3
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Data Reading and Storing in arrays

    Thanks so much for the help.

    Can i check with you again how do i know which n-th repetition they are from?

    how do i call the data out individually when i need to as it seems like they are stored in a list instead of arrays(for eg. i need the coordinates from the 3rd repetition)?

  5. #4
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Data Reading and Storing in arrays

    Assuming points is a QMap or QHash of <int, QVector3D>, you could say:

    Qt Code:
    1. points.insert(list.at(0).toInt(), vec);
    To copy to clipboard, switch view to plain text mode 

    Of course, if the iterations are not guaranteed to be unique, you might consider using insertMulti() instead.

  6. The following user says thank you to Urthas for this useful post:

    kango (6th January 2013)

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Data Reading and Storing in arrays

    Quote Originally Posted by kango View Post
    Can i check with you again how do i know which n-th repetition they are from?
    Item at index 'k' of the list is the 'k+1' repetition. If you want to store the index as well then simply replace QVector3D with your own structure containing four members -- index, x, y and z.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    kango (6th January 2013)

  9. #6
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Data Reading and Storing in arrays

    i tried to add a new structure but i'm experiencing a new error as it says i cannot use RepCount, setX, setY and setZ as function. can i know what is wrong with my code?

    Qt Code:
    1. struct FileData
    2. {
    3. double RepCount, setX, setY, setZ;
    4. };
    5.  
    6.  
    7. void Read(QString Filename)
    8. {
    9. QFile mFile(Filename);
    10. if(!mFile.open(QFile::ReadOnly))
    11. {
    12. qDebug()<< "could not open file for reading";
    13. return;
    14. }
    15.  
    16. QList<FileData> points;
    17. while(!mFile.atEnd())
    18. {
    19. QString line = mFile.readLine();
    20. QStringList list = line.split('|');
    21. FileData vec;
    22. vec.RepCount(list.at(0).toInt());
    23. vec.setX(list.at(1).toInt());
    24. vec.setY(list.at(2).toInt());
    25. vec.setZ(list.at(3).toInt());
    26. points << vec;
    27. }
    28.  
    29.  
    30. mFile.close();
    31. }
    To copy to clipboard, switch view to plain text mode 

    in addition, i'm still unsure of how to call out the individual values which i stored and then assign them to new objects( such as i want to name the X coordinate of Rep 2 and point it to Rep2coordX so that i can use Rep2coordX in future to do some calculations).

    Thanks alot for the help so far

  10. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Data Reading and Storing in arrays

    I'm sorry, this is no place to teach you C++ and object oriented programming. You need to learn the difference between a method/function and a member field of a structure.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    kango (6th January 2013)

  12. #8
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Data Reading and Storing in arrays

    sorry but can you help me by telling me where i'm wrong so that i can try to remedy it as it seems right to me?

  13. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Data Reading and Storing in arrays

    See the docs for QVector3D and compare it with your structure. There are no "setX", "setY" and "setZ" fields in QVector3D and there are no "setX", "setY" and "setZ" methods in your structure. The compiler already told you what is wrong with your code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    kango (6th January 2013)

Similar Threads

  1. Saving and storing data,QtSql
    By salmanmanekia in forum Newbie
    Replies: 7
    Last Post: 20th April 2010, 20:08
  2. Storing and reading back template types in QVariant
    By Daniel Dekkers in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2010, 08:40
  3. storing data in hexadecimal format
    By aj2903 in forum Qt Programming
    Replies: 6
    Last Post: 13th January 2010, 06:29
  4. Storing and managing huge arrays
    By maka in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2009, 10:37
  5. Storing/Reading item creation date
    By maverick_pol in forum Qt Programming
    Replies: 2
    Last Post: 26th May 2008, 09:19

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.