I am making a linked lists of words that appear in a file (no repeats) and the line they first appear on. I finished what I thought would be the hard part (parsing the file while keeping track of the lines), but I believe I now have a problem in one of my methods that i cant figure out how to fix. My code is in two files, but I'm only including the method with a problem in my code along with the driver. (I tried using GDB and I think I was using it wrong because it kept saying it couldn't find files and it wouldn't run. )
int main(int argc, char **argv){
file = fopen(argv[1],"r");
/*struct fileIndex *fIndex = NULL;*/ /*put this in header file??*/
fIndex = NULL;
delimiters = " .,;:!-";/*strtok chars to seperate*/
rewind(file);
int buffer = 65;
char str[buffer+1];/*where the lines are being stored*/
char *token, *cp;
int i;
int len;
while((fgets(str, buffer, file))!=NULL){/*inserting lines*/
for(i=0; i<buffer; i++){
if(str[i]=='\n'){
str[i]= '\0';
break;
}
}
len = strlen(str);
cp = xerox(str);
token = strtok(cp, delimiters);
/*if(token!=NULL)
printf("The word is %s\n", token);*/
if(!present(fIndex, token)&&(token!=NULL)){
insert(fIndex, i+1, token);
}
while(token!=NULL){
token = strtok(NULL, delimiters);
/*if(token!=NULL)
printf("The word is %s\n", token);*/
if(!present(fIndex, token)&&(token!=NULL)){
insert(fIndex, i+1, token);
}
}
}
fclose(file);
struct fileIndex *root;
root = fIndex;
while(root != NULL){
printf("The string is %s and on line %d\n", root -> str, root -> lineNum);
root = root -> next;
}
free(fIndex);
free(cp);
return 0;
}
struct fileIndex *insert(struct fileIndex *head, int num, char *insert){
struct fileIndex* newnode = malloc(sizeof(struct fileIndex));
if(newnode==NULL)
exit(1);
newnode -> str = insert;
newnode -> lineNum = num;
newnode -> next = head;
return newnode;
}
EDIT: I'm also thinking a problem in my method to check if a word is already there or not. I put a print statement where it should only print if the word is going to be inserted and all of the words printed. The little loop at the end to print the list is not printing and I think it reaches NULL when it first gets there and never loops.
present(struct fileIndex* fIndex, char *findIt){/*finds if word is in structure*/
struct fileIndex* current = fIndex;
while(current!=NULL){
current = current -> next;
if(strcmpigncase(current -> str, findIt)==0){
return current -> lineNum;
}
}
return 0;
}