| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Problem with pointers
Can't get my pointers working. I have head = tmp only when head = NULL, which loads the first piece of data from the file. But when the while loop finishes, head->key now contains the last piece of data in the file. How is the contents of head changing?
URL |
|
#2
|
|||
|
|||
|
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"; |
|
#3
|
|||
|
|||
|
Got it.
Someone pointed out that I had left the creation of tmp outside of the while loop, so head was just pointing to the first instance.
Disaster averted! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Problem with pointers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|