Input from file to linked list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • paranoidandroid
    New Member
    • Mar 2008
    • 3

    #1

    Input from file to linked list

    I have a txt file with a list of records for example:

    John Swift 15 2005 20000
    Andrew Smith 25 2001 25000

    I am trying to read in this data into a linked list of data structures as follows:

    Code:
    
    		temp = new client;
    		head = temp;
    		//do
    		while(!infile.eof())
    			{
    				infile >> temp->name;
    				infile >> temp->experience;
    				infile >> temp->year;
    				infile >> temp->desiredsalary;
    				
    				temp->next = new client;
    				temp = temp->next
    
    			} 
    			
    
    			temp->next = NULL; // set last next in the linked list to NULL
    			temp = head; // move temp pointer to start of the list
    		}
    The problem I'm having is that as I have shown in the text file, the name has a whitespace in between name and surname.

    I'm not very good with input / output and strings. Could someone please give me an idea as to how to enter both name and surname from the file into one variable in the structure of the linked list.

    I think the getline command has to be used:
    Code:
     infile.getline (name);
    I don't know how to set up delimiters to do this... any help greatly appreciated

    Thx!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I assume you know the format of the file to be read in.

    That is, the name has to be the same length in each record or you can't set the getline argument for the number of characters to read (the buffer size).

    Otherwise, the >> operator stops on whitespace so you only get one word of the name. If you know the name always has two words you can use the >> twice and fetch one word at a time.

    Also, I would separate your file I/O from your linked list.

    And lastly, since you are using C++ why are you re-coding a linked list rather than using the linked list container in the STL?

    Comment

    • paranoidandroid
      New Member
      • Mar 2008
      • 3

      #3
      Originally posted by weaknessforcats
      I assume you know the format of the file to be read in.

      That is, the name has to be the same length in each record or you can't set the getline argument for the number of characters to read (the buffer size).

      Otherwise, the >> operator stops on whitespace so you only get one word of the name. If you know the name always has two words you can use the >> twice and fetch one word at a time.

      Also, I would separate your file I/O from your linked list.

      And lastly, since you are using C++ why are you re-coding a linked list rather than using the linked list container in the STL?

      Hi!

      Thanks for your comments, appreciate your help.

      Unbfortunately the names vary both in actual length and the number of names given hence the difficulty.

      I'm recoding a linked list as this is how I am being asked to do this, that's one of the constraints as is the ability to use classes.

      Just to follow up on what you suggested about seperating the file i/o from linked list...why do you say that?


      Regards...

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        [paranoidandriod]
        Just to follow up on what you suggested about seperating the file i/o from linked list...why do you say that?
        [/quote]

        OK. Don't separate them. Let's say you spend 500 hours getting the code to work. Next week you need a linked list in another program that does not require a file. Now what?

        You can't use the code you have already written because it is intertwined with file I/O. So, you make a copy, delete the file I/O stuff and end up with two copies of the code to maintain. Then a third situation arises, so you copy the code again...

        Whenever you write code keep an eye open for reuseability. By separating the linked list from the file I/O you have linked list capability in any program by using the original debugged code.

        Comment

        • paranoidandroid
          New Member
          • Mar 2008
          • 3

          #5
          Thanks...that makes a lot of sense.

          Comment

          Working...