Why won't this work? (Linked List and text file)

This is a discussion on Why won't this work? (Linked List and text file) within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get some data from a text file into a linked list using pointers so I can print ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    20

    Why won't this work? (Linked List and text file)

    I'm trying to get some data from a text file into a linked list using pointers so I can print the data out. Here's the code I have.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string> //List is a list of names
    #include <cstddef> //For NULL
    
    using namespace std;
    
    struct nodeType
    {
    	string name[10];
    	nodeType* link;
    };
    	
    void CreateList(nodeType* &head, ifstream& infile);
    void PrintList(nodeType* head);
    
    int main()
    {
    	ifstream infile;
    	infile.open("infile.txt");
    
    	nodeType* head = NULL; 
    	
    	if(!infile)
    	{
    		cout << "The file does not exist.";
    		return 0;
    	}
    
    	CreateList(head, infile);
            PrintList(head);
    
                   return 0;
    }
    
    void CreateList (nodeType* &head, ifstream& infile)
    {
    	head = NULL;
    }
    
    //
    void PrintList (nodeType* head)
    {
    	nodeType* currptr = head;
    
    	while (currptr != NULL)
    	{
    		for(int i = 0; i < 10; i++)
    		       cout << head -> name[i] << endl; //I think this is the problem
    		currptr = currptr -> link;	
    	}
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Code:
    void CreateList (nodeType* &head, ifstream& infile)
    {
    	head = NULL;
    }
    What's the point of this?
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    20

    oops

    Code:
    void CreateList (nodeType* &head, ifstream& infile)
    {
    	head = NULL;
    
    	while(infile) 
    	{
    		for(int i = 0; i < 10; i++)
    			infile >> head -> name[i];
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  4. Transferring text from file into linked list
    By manutdfan in forum C Programming
    Replies: 41
    Last Post: 01-22-2007, 05:19 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21