-3
#include <stdio.h>
#include <stdlib.h>

typedef struct node{
    char word[20];
    struct node * next;
}node;

int main(){
    FILE *ifp;
    char newword[20];
    node * head;

    ifp = fopen("para.txt","r");
    head = (node * )malloc(sizeof(node));

    while(fscanf(ifp,"%s",newword) != EOF){
         head -> next = NULL;
         head -> word = newword;
     }

    return 0;
}

I want to add the words which is read by the text file to a link list. I tried to do with this code but I couldn't. How can I fix this.

Linuxios
  • 34,849
  • 13
  • 91
  • 116
Dulanjali
  • 27
  • 1
  • 7
  • 1
    1. [You should not cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) 2. Should check the return value from `fopen` – Ed Heal Jun 06 '18 at 18:23
  • Possible duplicate of [Assigning a char pointer to char array in C](https://stackoverflow.com/questions/29575109/assigning-a-char-pointer-to-char-array-in-c) – n. m. could be an AI Jun 06 '18 at 18:24

2 Answers2

1

You only allocate one node (head) and then change its contents each iteration of the loop. To create a linked list, you need to allocate a new node for each word (each iteration of the loop). Something like this should do it:

int main(){
    FILE *ifp;
    char newword[20];
    node * head = NULL;
    node  *last = NULL;
    node  *current;

    ifp = fopen("para.txt","r");
    if (ifp == NULL) {
        fprintf(stderr, "Unable to open file para.txt\n");
        return EXIT_FAILURE;
    }
    while(fscanf(ifp,"%19s",newword) != EOF){
         current = malloc(sizeof(node));
         strcpy(current -> word,newword);
         if(last) {
             last->next = current;
         }
         else {
             head = current;
         }
         last = current;
    }

    return EXIT_SUCCESS;
}
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
Linuxios
  • 34,849
  • 13
  • 91
  • 116
0

Keep track of a head and tail, or just push at head. The following appends to end, efficiently.

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
    struct node * next;
    char word[1];
    //or, char word[20];//fixed length word
}node;

node*
nodeNew(char* word) {
    if(!word) return NULL;
    //either dynamically allocate for strlen(word)
    node* pnode = malloc( sizeof(node)+strlen(word) );
    //or fixed len word 
    if( pnode ) {
        pnode->next = NULL;
        strcpy(pnode->word, word);
        //or, strncpy(pnode->word, word, 20-1); //fixed length
    }
    return pnode;
}

int main()
{
    FILE *ifp;
    char newword[200]; //since using fscanf, need to avoid buffer overflow, should use fgets and strtok instead
    node * head;

    if( !(ifp = fopen("para.txt","r") ) { printf("error\n"); exit(0); }
    head = NULL;

    while(fscanf(ifp,"%s",newword) != EOF){
        if( !head ) { tail = head = nodeNew(newword); }
        else { tail->next = nodeNew(newword);
    }
    //head points to first element, head->next points to next element
    //tail points to last element

    return 0;
}
ChuckCottrill
  • 4,360
  • 2
  • 24
  • 42