-
reading from a file
i have a list of names in a file but i dont know how many like
bob
joe
john
and i need them in different variables so my first idea was to use getline to put them into an array of pointers. but it just made the variables full of junk characters. any ideas on how i could do this
-
STL vector or list class templates?
-
You could make a linked list that will hold the names, and then infile the names in. Every new name gets its own node. For example:
Code:
char * name;
ifstream names("name.nms");
while (name!=NULL) // Keep bringing in names until file is ended
{
name >> name;
node * next = new node(name);
}
That should work, though that loop part may not you may have to work on that. Hope that helps.
Brendan
-
-
struct node
{
char name[80];
node *next;
}
class list
{
//code
public:
void addNode(node *);
//code
};
int main()
{
ifstream fin("myfile.txt")
list myList;
while(!fin.eof())
{
node * newNode = new node;
fin.getline(newNode->name, 79);
newNode->next = NULL;
myList.addNode(newNode);
}