> while(!feof(IN)){
> fgets(buffer, sizeof(buffer), IN);
Should be
while( fgets(buffer, sizeof(buffer), IN) != NULL ) {
Reason:
feof() doesn't return true when you've read the last character from the file, it returns true when some other read function (eg fgets) has returned true.
Code:
for(i = 0; i < 7; i++){
Rec[j].recordBuf[i] = malloc(60 * sizeof(char));
Rec[j].recordBuf[i] = token;
token = strtok(NULL, delim);
}
Should be
Code:
for(i = 0; i < 7; i++){
Rec[j].recordBuf[i] = malloc(60 * sizeof(char));
strcpy( Rec[j].recordBuf[i], token );
token = strtok(NULL, delim);
}
Rec[j].recordBuf[i] = token; does not copy the string, it just copies the pointer (and you lose the memory you allocated).
Code:
for(i = 0; i < atoi(Rec[j].recordBuf[6]); i++){
Rec[j].addr[i] = malloc(30 * sizeof(char));
fgets(Rec[j].addr[i], sizeof(Rec[j].addr[i]), IN);
}
Should be
Code:
for(i = 0; i < atoi(Rec[j].recordBuf[6]); i++){
Rec[j].addr[i] = malloc(30 * sizeof(char));
fgets(Rec[j].addr[i], 30, IN);
}
sizeof(Rec[j].addr[i]) is the size of the pointer (typically 4 bytes), not the size of the amount of memory it points to.