PDA

View Full Version : qstringlist problem



stella1016
17th December 2009, 08:36
I read data from file, line by line, and each will be stored as an element in a vector.
std::vector<one_screw*> m_recorded_screw;
Here is the code:


typedef struct{
int id;
QString name;
float x;
float y;
float z;
float dis;
QString checked;
QString tool;
}one_screw;

void read_file(...)
{
QTextStream in(&file);
for( QString line = in.readLine(); !line.isNull(); line = in.readLine())
{
line = line.trimmed();
one_screw *a_screw;

QStringList list = line.split(" ", QString::SkipEmptyParts);

a_screw->id = list.at(0).toInt();
a_screw->name = list.at(1); //->prolem
a_screw->x = list.at(2).toFloat();
a_screw->y = list.at(3).toFloat();
a_screw->z = list.at(4).toFloat();
a_screw->dis = list.at(5).toFloat();
a_screw->checked = list.at(6); //->prolem
a_screw->tool = list.at(7); //->prolem

m_recorded_screw.push_back(a_screw);
}
}


I am not sure whether I must use iterator to access the QStringList element. But for the marked lines I have run-time error "Access violation writing location...". In the struct I cannot use QString? Or I use QStringList wrongly? Help!

stella1016
17th December 2009, 08:41
One thing is missing that, I know the number of elements in one line. They are just one record, with words and numbers, but seperated by unknown number of spaces.
Maybe better idea instead of using split?

caduel
17th December 2009, 08:56
i) if you insist on storing pointers in that list, you need to fix line 18 to

one_screw *a_screw=new one_scree;
(or just store on_screw by value)
ii) I would add a check in line 20 to make sure that the list actually contains 7 elements.

HTH

stella1016
17th December 2009, 09:12
i) if you insist on storing pointers in that list, you need to fix line 18 to

one_screw *a_screw=new one_scree;
(or just store on_screw by value)
ii) I would add a check in line 20 to make sure that the list actually contains 7 elements.

HTH

Oh, thanks for the remind! During debugging, I changed it to pointer, but it is not necessary.
I will double check this. Thanks again.

stella1016
17th December 2009, 09:16
Now my problem is solved! :) stupid mistake, but happy.