Thread: reading from a file to linked list

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    5

    reading from a file to linked list

    I'm having a little trouble reading a file into a linked list (which I'm just learning). The code I have is:
    Code:
    	
    	struct stock
    	{
    		char isbnChar[10],
    			 bookNoChar[10], 
    			 borrowedFromDate[3], 
    			 loanLengthChar[3], 
    			 currentBorrowerNo[5], 
      			 lastBorrowerNo[5],
    			 status[2];
    		struct stock * next;
    		int j;
    
    	};
    
            stock *root = NULL;
    	stock *curr = NULL;
    
    	ifstream IssueBook("stock.txt", ios::in);
    	if (!IssueBook)
    		cerr << "File Ccould not be opened" << endl;
    	do
    	{
    		IssueBook.getline(curr->bookNoChar, 5, ',');
    		IssueBook.getline(curr->isbnChar, 10, ',');
    		IssueBook.getline(curr->borrowedFromDate, 3, ',');
    		IssueBook.getline(curr->loanLengthChar, 3, ',');
    		IssueBook.getline(curr->lastBorrowerNo, 3, ',');
    		IssueBook.getline(curr->currentBorrowerNo, 3, ',');
    		IssueBook.getline(curr->status, 2, '\n');
    		if (strcmp(curr->bookNoChar,bookNumChar))
    			strcpy(curr->status, "O");
    		curr->next = new stock;
    		curr = curr->next;
    	}
    	while( !IssueBook.eof()  );
    	IssueBook.close();
    It doesn't like IssueBook.getline(curr->bookNoChar, 5, ','); and crashes straight away.

    The code should read comma sepparated values from a file into the list and change one value (in the if statement). Then I'll traverse the list putting the values back into the file with the updated one ammended.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    curr is just a pointer. If you want to try and access something that it points to, you'll have to point it somewhere (at a stock instance created on the heap or stack).

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    Oh yeah sorry missed that bit of code. I've now put in
    root = new stock;
    I think I'm on my way now just hard getting used to it all


  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    Now I'm stumped as to why instead of it reading the values:
    1,1,100,21,***,***,S
    it reads
    1,1,100, ,21,***, ,

    Is there something wrong with my getline statements because they work ok in the rest of the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  3. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 06:09 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM