PDA

View Full Version : Data Reading and Storing in arrays



kango
3rd January 2013, 16:41
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.

wysota
3rd January 2013, 17:01
It's much simpler than that.


QFile f(...);
f.open(...);
QList<QVector3D> points;
while(!f.atEnd()) {
QString line = f.readLine();
QStringList list = line.split('|');
QVector3D vec;
vec.setX(list.at(1).toInt());
vec.setY(list.at(2).toInt());
vec.setZ(list.at(3).toInt());
points << vec;
}

Of course you need to provide some error checking.

kango
4th January 2013, 03:19
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)?

Urthas
4th January 2013, 06:14
Assuming points is a QMap or QHash of <int, QVector3D>, you could say:


points.insert(list.at(0).toInt(), vec);

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

wysota
4th January 2013, 09:45
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.

kango
6th January 2013, 14:28
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?


struct FileData
{
double RepCount, setX, setY, setZ;
};


void Read(QString Filename)
{
QFile mFile(Filename);
if(!mFile.open(QFile::ReadOnly))
{
qDebug()<< "could not open file for reading";
return;
}

QList<FileData> points;
while(!mFile.atEnd())
{
QString line = mFile.readLine();
QStringList list = line.split('|');
FileData vec;
vec.RepCount(list.at(0).toInt());
vec.setX(list.at(1).toInt());
vec.setY(list.at(2).toInt());
vec.setZ(list.at(3).toInt());
points << vec;
}


mFile.close();
}

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

wysota
6th January 2013, 14:38
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.

kango
6th January 2013, 15:10
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?

wysota
6th January 2013, 15:13
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.