Greetings I've been working on a homework in which I must create a linked list program which can save and restore its data from a text file. I have gotten most of it working but I'm having some problems with restoring the linked list using the data from the text file that was created from it. When I restore it the last element is repeated twice. I believe it has something to do with the loop but as I'm not sure I'm including the whole scope of my assignment.
Below is the code I've written so far of the program:
ListNode.h
#pragma once
template<class T> class List;
template<class T>
class ListNode
{
friend class List<T>;
public:
ListNode(const T&);
T getData()const;
private:
T data;
ListNode<T>*next;
};//end ListNode class
template<class T>
ListNode<T>::ListNode( const T &info):data(info), next(NULL)
{
}//end default constructor
template<class T>
T ListNode<T>::getData()const
{
return data;
}//end function getData
List.h
#pragma once
#include<iostream>
#include <fstream>
using namespace :: std;
#include "ListNode.h"
template<class T>
class List
{
private:
ListNode<T> *head;
int size;
ListNode<T> *find(int index) const;
public:
List();
List(const List<T> &aList);
~List();
int getLength() const;
void insert (int index, T tempData);
void remove(int index);
void retrieve(int index, T &tempData);
bool isEmpty() const;
void print() const;
void save();
void restore();
};//end List class
Broken function:
template <class T>
void List<T>::restore()
{
ifstream inFile;
inFile.open("listFile.txt");
T value=0;
int index, check =0;
cout << "Now reading the data from the text file..." << endl;
//inFile >> value;
//cout << "Value is : " << value << endl;
while (inFile != NULL)
{
inFile >> value;
index = getLength();
cout << value << endl;
insert(index+1, value);
inFile.close();
}
} // end function restore
The linked list data that was saved to the text file during the test was :
35
45
55
65
When the linked list was restored from the text file this was the content of it:
35
45
55
65
65
How can I solve this problem?