I am really having issues with getting linked list to work. My assignment requires us to open a txt file and read the words in to a linked list of structs. I thought i had my list sorted but i can not seem to read back from it. I have read the FAQ section here and whilst concise i was not able to find a solution. If anybody would have a second i would really appreciate if you could have a quick look at my code.
Code:
/* Structure definitions. */
typedef struct wordStruct* WordTypePtr;
typedef struct wordStruct
{
char* word;
unsigned count;
WordTypePtr nextWord;
} WordType;
typedef struct indexStruct
{
WordTypePtr headWord;
WordTypePtr longestWord;
WordTypePtr mostCommonWord;
unsigned totalWords;
unsigned uniqueWords;
} IndexType;
Code:
/* part of main */
int main(int argc, char* argv[])
{
IndexType index;
WordType * head;
index.headWord = head;
head=NULL;
char myString[MENU_SIZE+2];
int finished = FALSE;
int i;
int x;
int menuActive = TRUE;
char fname[99];
index.totalWords=0;
if (argc==1){
printf("\nError: No file specified\n");
system("PAUSE");
return EXIT_SUCCESS;
}
strcpy(fname,argv[1]);
x=loadData(&index, fname);
Code:
/****************************************************************************
* Loads all data into the index.
****************************************************************************/
int loadData(IndexType* index, char* trecFile)
{
WordType * curr, * head;
/*-------------------------*/
head=index->headWord;
char wordvar[99];
int c,j;
int d=0;
j=0;
FILE *fp;
printf("%s\n",trecFile);
/*Name of file */
/* File pointer- FILE defined in <stdio.h> */
fp = fopen(trecFile, "r"); /* File open function for reading */
/* Checking the return value of fopen() */
if ( fp == NULL) /* NULL defined in <stdio.h> */
{
fprintf(stderr, "\nUnable to open file %s\n", trecFile); /* Redirect output to stderr */
system("PAUSE");
return EXIT_SUCCESS;
}
else
{
/* Safe to read from file */
printf("\nFile %s open\n",trecFile);
while(( c = getc(fp)) != EOF)
{
if(c=='<'){
d=1;
}
if (d==1){
}else{
if(tolower(c)>96 && tolower(c) <123){
printf("%c",tolower(c));
wordvar[j]=tolower(c);
j++;
}
if (c==' '){
index->totalWords++;
curr = (WordType *)malloc(sizeof(WordType));
curr->count++;
curr->word=wordvar;
printf("\n%s\n",wordvar);
printf("%s\n",curr->word);
curr->nextWord = head;
/*printf(" pointer-> %c/n",head);*/
head = curr;
printf("\n");/*
j=0;*/
for(j=0;j<=99;j++) {
wordvar[j]=NULL;
}
j=0;
}
}
if(c=='>'){
d=0;
}
}
}
fclose(fp);/* closes stream fp */
printf("Total Words: %d\n\n",index->totalWords);
curr = head;
printf("%s\n",curr->word);
curr = curr->nextWord ;
printf("%s\n",curr->word);
for(j=0;j<=9;j++) {
printf("%s\n",curr->word);
curr = curr->nextWord ;
}
/*while(curr) {
printf("%s\n", curr->word);
curr = curr->nextWord ;
}*/
/*
curr = head;
while(curr) {
printf("%s\n", curr->word);
curr = curr->nextWord ;
}*/
return SUCCESS;
}