Hello everybody,
suppose I have a pascal type string "RecLen" (binary 06 00 00 00 52 65 63 4C 65 6E) and I want to skip it. I read the prepended length and use it on fseek like so:
fseek(stdin, readu32(), SEEK_CUR)
which works in part of my code. But sometimes it only skips "RecLe" and subsequent reads return nonsense.
I checked fseek is always returning zero (i.e. success) and readu32 of course is called before fseek (I checked the disassembly). I experimented with fflush(stdin) and ftell(stdin), because it may be a buffering problem or some optimization with the file position indicator (though I switched it off for bug hunting). I do not see a mistake, but it may be obvious to you. Please give me a hint.
Thank you for your time and for considering the problem.
PS: Just in case...
uint32_t readu32(void)
{
uint32_t ret;
if (fread(&ret, sizeof(uint32_t), 1, stdin) < 1) {
fputs("read error", stderr);
exit(-1);
}
return ret;
}