Hi,
I am trying to read in a file through a command line argument, then extract the words (no punctuation) and put each word into a node on a linked list, but am experiencing a few problems. Any help would be appreciated:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define LENGTH 30;
//Typedef for linked list.
typedef struct L {
char word[LENGTH] ;
struct L *next ;
} List;
//function to set up the list.
List *somelist( char word[], List *tl ) {
List *t = (List*) malloc (sizeof(List));
strcpy(t->word, word[LENGTH]) ;
t->next = tail;
return t;
}
//function to open and read the file and put data into the list.
List *insert( char head[LENGTH], LIST *tl ) {
//start clock
clock_t start, end;
float result;
start = clock();
//Open file
FILE *fd;
fd = fopen("file.txt", "r");
//Insert words into the list
while(!EOF)
while(ispunct(word)==0 && isspace(word)==0) {
char word[LENGTH] = fgetc(fd);
word[LENGTH] = tolower(word[LENGTH]);
somelist(word, z);
}
}
//end clock
end = clock();
result = ((end-start)/CLOCK_PER_SEC);
return z ;
}
//function to find the given word.
void *find( char *what[LENGTH], List *tlist ) {
int counter=0;
while( tlist != NULL ) {
if( tlist->x == what ) {
counter++;
}
tlist = tlist->next ;
}
printf("The number of occurunces of %s = %d", what[LENGTH], counter) ;
}
//main function.
int main( void ) {
//Initialise list
List *z = somelist();
List *pointer = NULL;
pointer = somelist(argv[1]);
char YesNo="y";
char c;
char array[LENGTH];
int counter;
printf("Enter word to be searched");
while(c!='\n') {
c=getchar();
c = tolower(c);
array[counter] = c;
counter++;
}
find(array[LENGTH], z);
}
Thanks, mbk.