Hi,
I am having problems with the following code, it seems to have gone into an infinite loop while trying to add current to the end of the list. I would much appreciate someone explaining where I have gone wrong and how I should fix it.
Thanks heaps
Code:#include<stdlib.h> #include<stdio.h> struct list_el { char *val; struct list_el * next; }; typedef struct list_el item; int main() { item *current, *head; head = NULL; current = NULL; // create file FILE *battingFile; // open file battingFile = fopen("BATTING.txt","r"); // error if cannot open if (battingFile == NULL) { printf("Can't open input file!\n"); } char line[300]; while (fgets(line, 300 ,battingFile)) { while(line[0] != '#') { //printf("%s", line); current = (item *)malloc(sizeof(item)); if(head == NULL) { // add info to front of list head = (item *)malloc(sizeof(item)); head->val = line; current = head; } else { // go to the end of list while(current->next != NULL) { current = current->next; } // add to end of list current->val = line; } } } return 1; }



LinkBack URL
About LinkBacks


