I would able to read all my struct bytes by bytes (block of bytes are large "sizeof(Today)" so it's 8 bytes large). I attempt more and more times, but I can't! Give me help please!
Bin file: "temp.bin"
My struct:
typedef struct
{
int year;
int month;
} Today;
And this is part of code that should be read 8 bytes each cycle:
Today *d;
d = malloc(sizeof(Today));
fp = fopen("temp.bin", "rb");
while(!feof(fp))
{
fread(d, sizeof(Today), 1, fp);
printf("Year = %i\n", d->year);
printf("Month = %i\n", d->month);
}
fclose(fp);
More probably is wrong the while condition and fread() function, but I tried all possible combination but the output is never the right one! Thank you all
freeaftermalloc. - cadanilukmalloc, but if you are allocating 1Todaytemporarily, it would be more straightforward to just define it locally asToday d;. Then you don't have to deal withmalloc/freehassle. Your structure is really small in size, so size shouldn't really be an issue. - user694733