I am reading the following file:
records.txt:
John Smith
10
20
Michael J Smith
15
20
Glennie Macarthur
30
40
Melia Ramsburg
23
45
Wallace Weiss
49
23
Emmy Rodi
50
90
Shaunta Sinkfield
29
85
Lyndsay Riser
79
68
Leilani Weldy
14
95
Jason Roye
59
67
German Hoy
43
68
Jesse Boster
88
29
...with the code....
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct SinglyLinkedList {
struct SinglyLinkedList *next;
char name[50];
int mid;
int final;
} SinglyLinkedList;
int tableSize;
int hashFunction(char *s) {
int hashValue;
for(hashValue = 0; *s != '\0'; s++) {
hashValue += *s;
}
return hashValue % tableSize;
}
int main() {
int i,j,k, found, distance, hashVal;
SinglyLinkedList **table;
SinglyLinkedList *temp, *ptr;
FILE *file;
char s[50];
// get table size from user
fgets(s, sizeof(s), stdin);
sscanf(s, "%d", &tableSize);
// initialize pointer
table = (SinglyLinkedList**)malloc(sizeof(SinglyLinkedList*));
for(i=0; i<tableSize; i++) {
table[i] = NULL;
}
//get data from file
file = fopen("records.txt", "r");
while(fgets(s, sizeof(s), file) != NULL) {
temp = (SinglyLinkedList*)malloc(sizeof(SinglyLinkedList));
s[strlen(s)-1] = '\0';
strcpy(temp->name, s);
fgets(s, sizeof(s), file); sscanf(s, "%d", &(temp->mid));
fgets(s, sizeof(s), file); sscanf(s, "%d", &(temp->final));
hashVal = hashFunction(temp->name);
if(table[hashVal] == NULL) {
table[hashVal] = (SinglyLinkedList*)malloc(sizeof(SinglyLinkedList));
strcpy(table[hashVal]->name, temp->name);
table[hashVal]->mid = temp->mid;
table[hashVal]->final = temp->final;
} else {
ptr = table[hashVal];
while(ptr->next != NULL) ptr = ptr->next;
ptr->next = (SinglyLinkedList*)malloc(sizeof(SinglyLinkedList));
strcpy(ptr->next->name, temp->name);
ptr->next->mid = temp->mid; ptr->next->final = temp->final;
}
free(temp);
} // end of while
// display whole data in Linked List
for(i=0; i<tableSize; i++) {
ptr = table[i];
while(ptr != NULL) {
printf("%d : %s, %d, %d\n", i, ptr->name, ptr->mid, ptr->final);
ptr = ptr->next;
}
}
}
The expected output is:
0 : Melia Ramsburg, 23, 45
1 : Michael J Smith, 15, 20
1 : Leilani Weldy, 14, 95
1 : Jesse Boster, 88, 29
2 : Wallace Weiss, 49, 23
2 : Shaunta Sinkfield, 29, 85
3 : John Smith, 10, 20
3 : Glennie Macarthur, 30, 40
3 : Emmy Rodi, 50, 90
3 : German Hoy, 43, 68
4 : Lyndsay Riser, 79, 68
4 : Jason Roye, 59, 67
However, if I put trailing lines of whitespace(like 10 blank lines after 29 in the end of file) at the end of the input file the following unexpected output occurs:
0 : Melia Ramsburg, 23, 45
0 : , 88, 29
0 : , 88, 29
0 : , 88, 29
1 : Michael J Smith, 15, 20
1 : Leilani Weldy, 14, 95
1 : Jesse Boster, 88, 29
2 : Wallace Weiss, 49, 23
2 : Shaunta Sinkfield, 29, 85
3 : John Smith, 10, 20
3 : Glennie Macarthur, 30, 40
3 : Emmy Rodi, 50, 90
3 : German Hoy, 43, 68
4 : Lyndsay Riser, 79, 68
4 : Jason Roye, 59, 67
What's going wrong (where does the 88
come from?) and how can I ignore blank lines in the file properly?