This could be done in a single line, not three seperate lines.Code:myfile >> structname.itemname; myfile >> structname.price; myfile >> structname.luxury;
Code:myfile >> structname.itemname >> structname.price >> structname.luxury;You write a loop, just like you did when you were saving the data.Code:But I'm wanting to enter all the items from the txt file into an ARRAY of STRUCTS.
I think your problem has less to do with reading into an array of struct, and more into what an array of struct is and how to manipulate one, regardless of whether you're reading or not.
Do you know how to declare an array of structs? If so, do you know how to access, say the third element of this array, and say, a member called "name"?
Similarly, what does this loop do?Code:struct foo { char name[30]; int age; }; int main() { foo myNames[10]; // an array of 10 foo;s }
So if you know the answers to this, then I don't see why the file read is such a problem.Code:for (int i = 0; i < 10; ++i) { foo[i].age = 20; }
Regards,
Paul McKenzie




Reply With Quote