Hello all,
I think am having problem getting the add_to_list function below to work.
I created it so that it accepts a pointer to a linked list, a pointer to a char array (string) and a number for occurrence of the word.

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

typedef struct wrd{
    char *word;
    int number;

    struct wrd *next;

} word;

word *add_to_list(word *list, char *input_word, int num);
void output_list_to_screen(struct wrd *strucPtr);

int main()
{
     FILE *pfile;
     struct wrd *head = NULL;
     char scanned_word[50];
     char inFile[100];

    printf("Please type the name of the input file: ");
    scanf("%s",inFile);

    pfile = fopen(inFile, "r");
    do
    {
        fscanf(pfile, " %s ", scanned_word);
        printf("%s\n", scanned_word);

        head = add_to_list(head,scanned_word, 1);

    }while (feof(pfile) == 0);

    output_list_to_screen(head);
    return 0;
}
/*Adds elements to the linked list*/
struct wrd *add_to_list(word *list, char *input_word, int num)
{
    struct wrd *new_node;
    new_node=(word*) malloc(sizeof(struct wrd));
    new_node->word = input_word;
    new_node->number =num;
    new_node->next= list;

    return new_node;
}
/*Outputs the contents of the linked list.*/
void output_list_to_screen(struct wrd *strucPtr)
{
    struct wrd *list = strucPtr;
    while(list !=NULL)
    {
        printf("%s : %d\n",list->word,list->number);
        list = list->next;
    }
}

Output is:

Please type the name of the input file: test.txt
this
is
a
test
this
is
a
test
test : 1
test : 1
test : 1
test : 1
test : 1
test : 1
test : 1
test : 1

Process returned 0 (0x0)   execution time : 4.735 s
Press any key to continue.

For the txt file that contains:

this is a test
this is a test

Here is an example of a working one.(Though this one accept the address to the top of the linked list.)

student *add_list(student **ps, char *n, int ag)
{
    student *temp = malloc(sizeof(student));
    temp->name = n;
    temp->age =ag;
    temp->next= *ps;

    *ps = temp;
    return *ps;
}

int main(int args, char *argc[])
{
    student *head = NULL;

    add_list(&head, "Jim", 21);
    add_list(&head, "John", 27);
    add_list(&head, "Henry", 28);

    while(head)
    {
            printf("%s : %d\n", head->name, head->age);
            head = head->next;
    }
    return 0;
}

Now that I am thinking about it, could it be the char "input_word" pointer that is getting passed to the add_to_list function that is giving me problems, because it is the same pointer every time?

Any help would be appreciated.
Thanks.

Yep as I thought the pointer "input_word" was the problem.
I have now changed the struct to have an array member and now I am using strcpy in the add_to_list function as show below:

typedef struct wrd{
    char word[50];
    int number;

    struct wrd *next;

} word;

struct wrd *add_to_list(word *list, char *input_word, int num)
{
    struct wrd *new_node;
    new_node=(word*) malloc(sizeof(struct wrd));
    strcpy((new_node->word),input_word);
    new_node->number =num;
    new_node->next= list;

    return new_node;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.