Here is the program:
FILE *fp;
struct record rec;
while (fread((char *)&rec, sizeof(rec), 1, fp) == 1) // Here
{
/* do something to rec */
if (/*rec must be rewritten*/)
{
fseek(fp, -(long)sizeof(rec), 1);
fwrite((char*)&rec, sizeof(rec), 1, fp); // And here
}
}
Look at the notes, the author says:
It looks reasonable enough at first glance:
&rec
is carefully cast tochar *
to pass tofread
andfwrite
...
I can't understand the meaning.
Firstly, don't attempt to use SO **
markup inside code blocks. It doesn't work as intended and it only creates confusion.
Secondly, the book is apparently outdated. Back in the very old versions of C language there was no void *
type and char *
type was typically used instead as "generic" pointer type. The first parameter of fread
was declared with char *
type. For this reason it was customary to perform explicit cast of actual argument type to char *
to avoid compiler warnings. It might even be necessary today, if an old version of standard library is used (one that declares fread
with char *
parameter).
Modern C standard library declares fread
with first parameter of type void *
. No cast is necessary in that case and cast to char *
makes no sense at all.