text cover

Data Abstraction and Problem Solving with C++

Walls and Mirrors

by Frank M. Carrano

Addison Wesley Logo

example209.cpp

Go to the documentation of this file.
00001 
00016 // Creates a linked list from the data in a text
00017 // file.  The pointer variables head and tail are
00018 // initially NULL.  fileName is a string that names
00019 // an existing external text file.
00020 
00021 ifstream inFile(fileName);
00022 int      nextItem;
00023 
00024 if (inFile >> nextItem) // Is file empty?
00025 {  // File not empty:
00026    try
00027    {  head = new Node;
00028       // Add the first integer to the list.
00029       head->item = nextItem;
00030       head->next = NULL;
00031       tail = head;
00032 
00033       // Add remaining integers to linked list.
00034       while (inFile >> nextItem)
00035       {  tail->next = new Node;
00036          tail = tail->next;
00037     tail->item = nextItem;
00038     tail->next = NULL;
00039       }  // end while
00040    }  // end try
00041    catch (bad_alloc e)
00042    {  throw ListException(
00043       "ListException: restore cannot allocate memory.");
00044    }  // end catch
00045 }  // end if
00046 
00047 inFile.close();
00048 
00049 // Assertion: head points to the first node of the
00050 // created linked list; tail points to the last
00051 // node. If the file is empty, head and tail are
00052 // NULL (list is empty).

Generated on Sun Aug 27 13:05:37 2006 for AWLogo by  doxygen 1.4.6