I'm having trouble using fread() to convert a binary file into a linked list of structs.
The struct:
struct MdbRec {
char name[16];
char msg[24];
};
Relevant code:
FILE *file;
file = fopen( argv[1], "rb" );
struct List db;
initList(&db);
struct MdbRec *data = (struct MdbRec *)malloc(sizeof(struct MdbRec));
struct Node *last;
while( fread( data, 40, 1, file ) )
{
struct MdbRec *message = (struct MdbRec *)malloc(sizeof(struct MdbRec));
message = data;
if( !db.head )
{
last = addFront( &db, message );
db.head = last;
}
else
last = addAfter( &db, last, message );
if( fseek( file, 40, SEEK_CUR ) != 0)
break;
printf("read\n");
}
free(data);
removeAllNodes( &db );
addFront() and addAfter are methods of the linkedlist structure that mallocs space the data field.
When I run it with Valgrind it shows that I'm successfully having 2 allocations of memory. One is obviously the data variable. The other 568 bytes and it's very confusing to me. Valgrind says the error is coming from when I run fread().