linked list getline

This is a discussion on linked list getline within the C Programming forums, part of the General Programming Boards category; hey im trying to read a sequence of strings from the user and store them in different nodes..my code reads ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    84

    linked list getline

    hey im trying to read a sequence of strings from the user and store them in different nodes..my code reads the line and stores it as a whole sentence , how do i split the line into individual strings and store them into different nodes ? thanks!

    Code:
            getline(words)
            p=(Node *) malloc(sizeof(Node));
    	strcpy(p->word,words);
    	p->next = head;
    	head = p;
    Code:
     char *getline( char *text)
    {
    	if( fgets( word, 128, stdin ) == NULL ) return NULL;
    	if( ( temp = strchr( words, '\n' ) ) )
    		*temp = 0;
    	else while( getchar() != '\n' );
    	return words;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    A for loop and strtok() perhaps?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    how do i do that ? i mean where ? if i use strtok in getline , it will only return the first word , how do i store the words into the list ????
    Last edited by rocketman03; 11-23-2008 at 02:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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