Hello all, this is my first post so please for give if I make some type of posting mistake. I am in a programming in C class and have been given an assignment to make a program that opens a file "document.txt" and goes through each word in the list and counts how many of each word is in the file and then prints out the results in order. I have to use the started file that the instructor provide so it has to be done in the way and order that he as specified. It must use a link listed and functions and structs. I have gotten all the functions I am just having a problem with how to retreave the words. I get as far as the first word and that is it.
Any help I could get would be greatly appreciated
Here is my code:
Code:
// File: LinkedList.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct Node {
char Word[25];
int Count;
struct Node *Next;
};
/* Function Prototypes */
void GetWord( FILE *fileptr, char word[] );
struct Node* MakeNewNode( char word[] );
struct Node* InsertAtBeginning( struct Node *start, struct Node *newptr );
void InsertInMiddle( struct Node *newptr, struct Node *current, struct Node *previous );
void PrintList( struct Node *start );
int main()
{
/* declare and initialize variables */
FILE *fileptr;
char word[25];
struct Node *start = NULL;
struct Node *newptr = NULL;
struct Node *current = NULL;
struct Node *previous = NULL;
printf("Openning file...\n");
if( ( fileptr = fopen( "document.txt", "r" ) ) == NULL )
{
printf("Unable to open file...\n");
return;
}
newptr = MakeNewNode(word); // function call for MakeNewNode
/* read a word from the file */
GetWord( fileptr, word);
/* read words and process until end of file */
/* Set current pointer to the beginning of the list */
start = InsertAtBeginning( start, newptr); // fuction call InsertAtBeginning
/* Search to see if the word already exists in the list */
while(current != NULL && strcmp( current->Word, word) < 0)
{
current = current->Next;
previous=previous->Next;
}
//if(current == NULL)
//else
if (strcmp (current-> Word, word == 0))
{
current->Count ++;
}
else
newptr = MakeNewNode;
InsertInMiddle( newptr, current, previous );
/* First check to see if it goes before the first word */
/* check if it matches the first word */
/* if not at the beginning, it goes in the middle or end */
/* read next word from the file */
GetWord( fileptr, word ); // Function call for GetWord
PrintList( start ); // Function call for PrintList
printf("\n-------------------------------\n\n");
fclose( fileptr );
return;
}// end Main function
////////////////////////////////// Start Functions ////////////////////////////////////////////
//Fuction Definition for GetWord Function
void GetWord( FILE *fileptr, char word[] )// function call for GetWord
{
char tempword[25] = "";
int i = 0;
fscanf( fileptr, "%s", tempword );
if( !feof(fileptr) )
{
strcpy( word, strtok( tempword, " ,.;:!?-()" ));
while( word[i] != '\0' )
{
word[i] = tolower(word[i]);
i++;
}
printf( "%s\n", word );
}
return;
}// end GetWord Function
// Fuction Definition for InsertAtBeginning Function
struct Node* InsertAtBeginning( struct Node *start, struct Node *newptr )
{
newptr->Next = start;
start = newptr;
return start;
}// end InsertAtBeginning Function
// Fuction Definition for InsertInMiddle Function
void InsertInMiddle( struct Node *newptr, struct Node *current, struct Node *previous )
{
previous->Next = newptr;
newptr->Next = current;
return;
}// end InsertInMiddle Function
// Fuction Definition for MakeNewNode Function
struct Node* MakeNewNode( char word[] )
{
struct Node * ptr;
ptr = malloc( sizeof ( struct Node ));
strcpy( ptr->Word, word);
ptr->Count = 1;
ptr->Next=NULL;
return ptr;
}// end MakeNewNode Function
// Fuction Definition for PrintList Function void PrintList( struct Node *start )
{
printf(" Word Count\n");
printf("--------------- -----\n");
while( start != NULL )
{
printf("%15s %5d\n", start->Word, start->Count);
start = start->Next;
}
return;
}// end PrintList Function