Populating a class array, from a file...
Hi,
I currently have Character class that consists of about 30 variables (ints, doubles, and chars).
I''m planning that all the characters in the game will be using this Character class. I''m possibly looking at about 40 to 50 characters. So I''m going to need to make the Character class an array, so each of the characters can use the class.
All the equivalent data is located in a seperate text file. There are no variables in the text file just data (numbers and names). There is one piece of data per line.
I''m using MSVC++ 6 and I''m doing the game as WIN32 API (not console).
My problem is that I have no idea how to populate my class array with the data from the text file for all 40 to 50 of my characters.
If anyone can help me out i''d appreciate it. Also, if possible, go into as much detail as possible...i''m pretty new at this...
Thank you in advance!
Make the character class like you said. It has data elements in it, and functions to get at the different pieces of data. Now outside of this class, create another class that is the character manager class. This class contains the array of character class objects, with functions to load and save the whole shebang to/from file. You could use a regular array, a linked list, or whatever type of container is most appropriate (I would say STL vector but you may not be up on those so regular array is prolly fine).
Now, in the character class add functions ToFile and FromFile, which only write/read the one character contained in the class. I would think that c++ iostream woud be the way to go there, but of course fopen, fread, fwrite, etc will work fine for that too. Finally in the character manager class''s functions just go through each of the character objects in the array sequentially and write/read them to/from the file.
Having classes of objects and then other classes for managers of those objects is a very common design practice in c++.
Hope that helps
Douglas Sutherland
Now, in the character class add functions ToFile and FromFile, which only write/read the one character contained in the class. I would think that c++ iostream woud be the way to go there, but of course fopen, fread, fwrite, etc will work fine for that too. Finally in the character manager class''s functions just go through each of the character objects in the array sequentially and write/read them to/from the file.
Having classes of objects and then other classes for managers of those objects is a very common design practice in c++.
Hope that helps
Douglas Sutherland
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement