I have a binary file that has a large number of records in it. All I need to do is open the file and read it into memory. I am new to programming so bare with me. When my program reaches the fread statement, I get a runtime error that says “EXC_BAD_ACCESS”. I'm using Xcode on GCC, configured for ANSI-C. Can someone help me with my logic?
Code:
extern int read_recs_from_bin( char * binfile, data_p_t * zips_ptr, int * num_recs )
{
int count;
int inx;
int rcode = TRUE;
FILE *file = NULL;
errno = 0;
if ( (file = fopen( binfile, "rb" )) == NULL )
fprintf(stderr,
"Open file error: %d, \"%s\"\n",
errno,
strerror( errno )
);
else
{
fseek( file, 0, SEEK_END );
count = ftell( file ) / sizeof(ZIPS_data_p_t);
fseek( file, 0, SEEK_SET );
file = calloc(count, sizeof(ZIPS_data_p_t));
if (file == NULL)
exit (1);
for (inx = 0; inx < count; inx++)
{
fread(zips_ptr, sizeof(data_t), 1, &file[inx]);
}
free(file);
}
return rcode;
}