1

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?

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
falnir
  • 37
  • 2
  • 6
  • 1
    WOW That's a lot of code. http://sscce.org/ and/or http://http://ideone.com/ – Mooing Duck Feb 08 '12 at 00:08
  • 1
    Please don't dump your entire code in a question like that. Only post the relevant bits. Code dumps like this are not inviting to read. – Tony The Lion Feb 08 '12 at 00:08
  • 1
    Do you know how to use a debugger? If so, you should use it to get some insight about the problem. If not, you should *really* learn that. It's an essential tool for a programmer. – R. Martinho Fernandes Feb 08 '12 at 00:08
  • 2
    possible duplicate of [reading a line in text file twice](http://stackoverflow.com/questions/5466925/reading-a-line-in-text-file-twice) – Mooing Duck Feb 08 '12 at 00:09
  • 1
    I notice you are closing the file handle in the read in loop, you should fix that. – RussS Feb 08 '12 at 00:11
  • My answer got bombed but you might just want to move the inFile.close() outside the reading iteration. Move it after your while } – Captain Giraffe Feb 08 '12 at 00:25
  • Thanks RussS and Captain Giraffe. I moved it outside the while. It was still happening though. Tried the answer below and it worked. – falnir Feb 08 '12 at 00:32

3 Answers3

1

It amazes me that your restore function works at all. But yes, it's repeating the last element. Try this instead:

while(inFile >> value)
  ...

EDIT:
All right, just try this much:

while(inFile >> value)
{
  cout << value << endl;
}

Does it display the values correctly?

Beta
  • 96,650
  • 16
  • 149
  • 150
-1

I had this problem a while ago with one of my programs. It took me a while to figure out and I am not sure if it is the same problem, but check your input file one more time. If you leave an extra line after the last number, the compiler will return the last number twice.

example:

1

2

3

4

5

(extra line)

Just delete the extra line.

-1

Which one is the head of your list? You can use a while loop or even an if statement to traverse through each node and load them all.