-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.

2

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Shouldn't current -> word = newword; use strcpy?
@EdHeal: Yes, thanks; edit was made by krpra (thank you).
Works until strlen(word)>20, and since strcpy used, overwrites buffer
I have added a check to ensure no buffer overrun and fopen check.
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.