I'm trying to write a program which reads a .bin file into dynamically allocated structs. It seems to be doing what I want, up until I need to print the results. I'm met with an endless loop of the same struct over and over again, instead of each struct just once. The code is as follows:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
main(){
//File pointer
FILE *binFile = NULL;
//Struct declaration
typedef struct flightData_struct {
char flightNum[7];
char origCode[5];
char destCode[5];
int timeStamp;
struct flightData_struct* next;
} flightData;
flightData myFlightData;
struct flightData_struct *head;
struct flightData_struct *tail;
//Opening file
printf("Opening file...\n");
binFile = fopen("acars.bin", "rb");
if (binFile == NULL){
printf("ERROR: Could not open file. Please try again.");
return -1;
}
//Malloc'ing first struct
struct myflightData *point = (struct myFlightData*) malloc(sizeof(flightData));
fread(point, sizeof(flightData), 1, binFile);
head = point;
tail = point;
//Malloc'ing structs
while (!feof(binFile)){
flightData *temp = (struct flightData*) malloc(sizeof(flightData));
fread(temp, sizeof(flightData), 1, binFile);
temp->next = NULL;
tail->next = temp;
tail = tail->next;
}
tail = head;
while (tail != 0){
int t;
t = tail->timeStamp;
time_t time = t;
printf("%s| %s| %s| %s\n\n", tail->flightNum, tail->origCode, tail->destCode, asctime(gmtime(&time)));
}
//Closing file
printf("Closing file...");
fclose(binFile);
return 0;
}
And this is the output I get from it (it seems to loop infinitely).