Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7432213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:25:23+00:00 2026-05-29T09:25:23+00:00

Greetings I’ve been working on a homework in which I must create a linked

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:25:25+00:00Added an answer on May 29, 2026 at 9:25 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, I have a particular object which can be constructed from a file, as
Greetings everyone. Can any one has a working example for the CoreTelephony framework? I
Greetings , I am a new developer in BlackBerry Application. I have been working
Greetings! I'm working on wrapping my head around LINQ. If I had some XML
Greetings! I am trying to check directory write-permissions from within a Windows MFC/ATL program
Greetings, The VBA code below will create an Excel QueryTable object and display it
Greetings, I'm working on a game project that uses a 3D variant of hexagonal
Greetings Stack Overflow community. I'm trying to make a function that can pop all
Greetings, I've taken over from a prior team and writing ETL jobs which process
Greetings, We can edit image attributes with this get_image_tag filter when we are adding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.