Cask
Fledgling Freddie
- Joined
- Dec 27, 2003
- Messages
- 653
Bit of a C++ prob not sure if it's something the compiler is doing or something in my code.
Basically my while loop is reading the file structure data and saving it in tmp->key and tmp->recordnum. I haven't finished the binary tree structure that I want it to insert the data into, but so far it puts the first entry that it reads into head->key and head->recordnum. The problem is that once it has finished reading the data, the head now points to the last structure that it read instead of the first. Somehow it's getting tmp and head confused.
It's a real mind fuck, hope someone can help.
Code:
void updateindex() {
int ordNo;
ordNo = 0;
ifstream inputPathway;
inputPathway.open("orderfile.txt", ios::in);
if (!inputPathway.is_open() ) {
cout << "No orderfile present. A new one will be constructed.\n\n"; // checks if file exists to
return; // prevent infinite loop
} //
Link tmp;
tmp = new index;
inputPathway.read(reinterpret_cast<char*> (&order1), sizeof(order));
while (!inputPathway.eof()) { // build binary tree index here
strcpy(tmp->key,order1.name);
tmp->recordnum = order1.ordernum;
tmp->left = NULL;
tmp->right = NULL;
if (head == NULL) { // first node in a new index is placed at head
head = tmp;cout << head->key << " has been put at head"; }
inputPathway.read(reinterpret_cast<char*> (&order1), sizeof(order));
ordNo++;
ordertotal = order1.ordernum;
}
cout << "\nThere are " << ordNo << " orders on record\n";
inputPathway.close();
cout << head->key << " is now at head";
Basically my while loop is reading the file structure data and saving it in tmp->key and tmp->recordnum. I haven't finished the binary tree structure that I want it to insert the data into, but so far it puts the first entry that it reads into head->key and head->recordnum. The problem is that once it has finished reading the data, the head now points to the last structure that it read instead of the first. Somehow it's getting tmp and head confused.
It's a real mind fuck, hope someone can help.