6 Replies - 12094 Views - Last Post: 09 May 2010 - 02:14 PM Rate Topic: -----

#1 Guest_Rob*


Reputation:

Read From File into a Linked List

Posted 09 May 2010 - 01:36 PM

I am having difficulty reading into a linked list information from a text file. When I run the program I am using while(!dataFile.eof()) to keep my loop going but it is only reading in the top four lines in my text file. I need it to continue loading text from the file into new structures until it reaches the end of the file. I have pasted my function below. Any suggestions would be appreciated.

void addLoadEntry(cdStruct* start)	
		{
			fstream dataFile("output.txt", ios::in);
					
			//while(!dataFile.eof());
			{
				cdStruct* newStruct = new cdStruct;
				cdStruct *newEntry = newStruct;  //getnewstruct()
				getline(dataFile, newStruct->title);
				getline(dataFile, newStruct->artist);
				dataFile >> newStruct->numberOfSongs;
				dataFile >> newStruct->number;
				cdStruct *ptr = start;
				while(ptr->next) // while the current entry points to ANOTHER entry
				ptr = ptr->next;
				// at this point ptr is a valid entry that points to null so it is the end (or tail) of the list
				ptr->next = newEntry;
				ptr->next->next = NULL;
			}
				dataFile.close();
	
	}

This post has been edited by JackOfAllTrades: 09 May 2010 - 02:09 PM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0

Replies To: Read From File into a Linked List

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Read From File into a Linked List

Posted 09 May 2010 - 01:39 PM

have no idea of this works, but see the logic
void addLoadEntry(cdStruct* start)	
{
	ifstream dataFile("output.txt", ios::in);
        if(!dataFile) //file not open
            return; //return back to main

	while(!dataFile.eof());
	{
		cdStruct* newStruct = new cdStruct;
		getline(dataFile, newStruct->title);
		getline(dataFile, newStruct->artist);
		dataFile >> newStruct->numberOfSongs;
		dataFile >> newStruct->number;

		if(start == NULL)
		{
			start = newStruct; //if the first node (first link) is null, set the memory there
			start->next = NULL;
			return; //get out of the function
		}

		cdStruct *ptr = start;
		while(ptr->next != NULL) // loop through all the nodes to until you hit the end
			ptr = ptr->next;

                //ok now were are at the very last node
		ptr->next = newStruct;
		ptr->next->next = NULL;
	}
	dataFile.close();

} 


This post has been edited by ImaSexy: 09 May 2010 - 01:40 PM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Rob*


Reputation:

Re: Read From File into a Linked List

Posted 09 May 2010 - 01:51 PM

Thanks for the help! I've been trying to solve this problem for a few hours now and a textbook that I have is not much help. It seems to be getting caught in a possible infinite loop. I tried your modifications but same problem. I enclosed the whole code with my function commented out to see if that helps any.

#include <iostream>
	#include <string>
	#include <fstream>
	using namespace std;

	struct cdStruct //Structure containing Cd Database Elements
		{
			string title;
			string artist;
			int numberOfSongs;
			int number;	
			cdStruct *next;
		};

	void save(cdStruct *ptr)	//Function that saves database info to file
		{
			ofstream dataFile;
			dataFile.open("output.txt", ios::out | ios::app);
			dataFile << ptr->title << endl;
			dataFile << ptr->artist << endl;
			dataFile << ptr->numberOfSongs << endl;
			dataFile << ptr->number << endl;
			dataFile.close();			
		}
	
	void saveStructList(cdStruct *start)	//Function that passes database info to the save function
	
		{
			ofstream dataFile;
			dataFile.open("output.txt",ios::trunc);
			dataFile.close();
			cdStruct *ptr = start->next;
			while(ptr)
				{
					save(ptr);
					ptr = ptr->next;
				}
		}
		
void addLoadEntry(cdStruct* start)      
{
        ifstream dataFile("output.txt", ios::in);
        if(!dataFile) //file not open
            return; //return back to main

        while(!dataFile.eof());
        {
                cdStruct* newStruct = new cdStruct;
                getline(dataFile, newStruct->title);
                getline(dataFile, newStruct->artist);
                dataFile >> newStruct->numberOfSongs;
                dataFile >> newStruct->number;

                if(start == NULL)
                {
                        start = newStruct; //if the first node (first link) is null, set the memory there
                        start->next = NULL;
                        return; //get out of the function
                }

                cdStruct *ptr = start;
                while(ptr->next != NULL) // loop through all the nodes to until you hit the end
                        ptr = ptr->next;

                //ok now were are at the very last node
                ptr->next = newStruct;
                ptr->next->next = NULL;
        }
        dataFile.close();

} 

/*
	void addLoadEntry(cdStruct* start)	//Function that creates a new structure
		{
			fstream dataFile("output.txt", ios::in);
					
			//while(!dataFile.eof());
			{
				cdStruct* newStruct = new cdStruct;
				cdStruct *newEntry = newStruct;  //getnewstruct()
				getline(dataFile, newStruct->title);
				getline(dataFile, newStruct->artist);
				dataFile >> newStruct->numberOfSongs;
				dataFile >> newStruct->number;
				cdStruct *ptr = start;
				while(ptr->next) // while the current entry points to ANOTHER entry
				ptr = ptr->next;
				// at this point ptr is a valid entry that points to null so it is the end (or tail) of the list
				ptr->next = newEntry;
				ptr->next->next = NULL;
			}
				dataFile.close();
	
	}
*/

	void printStruct(cdStruct *ptr)	//Function that prints database
		{
			cout << "Title: " << ptr->title << endl;
			cout << "Artist: " << ptr->artist << endl;
			cout << "# of Songs: " << ptr->numberOfSongs << endl;
			cout << "ID #: " << ptr->number << endl;
		}

	void printStructList(cdStruct *start) //Function that sends database information to print function
		{
			cdStruct *ptr = start->next;
			while(ptr)
			{
				printStruct(ptr);
				ptr = ptr->next;
			}
		}

	void modifyStruct(cdStruct *start, int find)	//Function to modifiy an existing database element
	{
		cdStruct *current;
		cdStruct *previous;
		
		current = start->next; // Start the Current to the first real rec
		previous = start; // Start points to the first real rec
		while(current)
		{
			if(current->number == find) // Modifies the entry
				{
					cout << "Enter Cd Title: ";
					getline(cin, current->title);
					cout<< "Enter Artist Name: ";
					getline(cin, current->artist);
					cout << "Enter # of Songs: ";
					cin >> current->numberOfSongs;
					current->number = find;
					return;
				}

			else
				{
					previous = current;
					current = current->next; 
				}
			// This code bumps both the previous and current 
			// pointers one step down the line
		}
		
	}

	cdStruct *getNewStuct()	//Function to dynamically create a new structure as needed
		{			
			cdStruct* newStruct = new cdStruct;
			cout << "Enter Cd Title: ";
			getline(cin, newStruct->title);
			cout<< "Enter Artist Name: ";
			getline(cin, newStruct->artist);
			cout << "Enter # of Songs: ";
			cin >> newStruct->numberOfSongs;
			cout<< "Enter Id # ";
			cin >> newStruct->number;
			return newStruct;
		}

	void addEntry(cdStruct* start)	//Function that creates a new structure
		{
			cdStruct *newEntry = getNewStuct();
			cdStruct *ptr = start;
			while(ptr->next) // while the current entry points to ANOTHER entry
				ptr = ptr->next;
			// at this point ptr is a valid entry that points to null so it is the end (or tail) of the list
			ptr->next = newEntry;
			ptr->next->next = NULL;
		}

	int main()
		{
			cdStruct *start = new cdStruct; // first element will NOT handle any info
			start->next = NULL;
			int temp, userChoice;
			do
			{
				// print menu
				cout << "------- Super CD 2.0 --------" << endl << endl;
				cout << "1. Print CD Database" << endl;
				cout << "2. Create a New Entry" << endl;
				cout << "3. Modify an Existing Entry" << endl;
				cout << "4. Save Database" << endl; 
				cout << "5. Load Database" << endl;
				cout << "6. Exit " << endl << endl;
				cout << "Your Selection -> ";
						
				cin >> userChoice;
				cin.ignore(200,'\n');
				switch(userChoice)
				{
					case 1:	//print user mendu
						printStructList(start);
						break;
				
					case 2:	//allow user to make a new entry
						addEntry(start);
						break;

					case 3:	//allow user to modify an existing entry
						cout<<"Enter number to be modified: ";
						cin >> temp;
						cin.ignore(200,'\n');
						modifyStruct(start, temp);
						break;
						
					case 4:	//allow user to save the database to file
						saveStructList(start);
						break;

					case 5:	//allow user to load an existing database
						addLoadEntry(start);
						break;

					case 6:	//allow user to exit the program
						cout << endl << "You Chose to Exit" << endl;

				} // switch
			}while(userChoice != 6);
			
			return 0;
		}

This post has been edited by JackOfAllTrades: 09 May 2010 - 02:09 PM
Reason for edit:: Added code tags.

Was This Post Helpful? 0

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Read From File into a Linked List

Posted 09 May 2010 - 02:02 PM

when you use a while loop to find where the last node is your actually checking all of the allocations within your linked list

example
:
while(ptr->next != NULL)
	ptr = ptr->next;



which means :

keep looping while the link in front of mine exist and is allocated, stop looping when i hit a link in my chain that is not allocated (which should be the very end of your list)

Now your ptr->next points to the very last link you can now allcated memory for that position in your chain.


void addEntry(cdStruct* start)	//Function that creates a new structure
{
	cdStruct *newEntry = getNewStuct();
	cdStruct *ptr = start;
	while(ptr->next != NULL)
		ptr = ptr->next;
	ptr->next = newEntry;
	ptr->next->next = NULL;
}



Where are you hitting a infinite loop?

This post has been edited by ImaSexy: 09 May 2010 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#5 Guest_Guest*


Reputation:

Re: Read From File into a Linked List

Posted 09 May 2010 - 02:04 PM

Cool, thanks again for the help!
Was This Post Helpful? 0

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,417
  • Joined: 23-August 08

Re: Read From File into a Linked List

Posted 09 May 2010 - 02:10 PM

When posting code, please use code tags.

:code:
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Guest*


Reputation:

Re: Read From File into a Linked List

Posted 09 May 2010 - 02:14 PM

Sorry, didn't see your question. I think it is hitting an infinite loop when trying to load from the file. The program freezes at that point.
Was This Post Helpful? 0

Page 1 of 1