I can't understand this code
Code:
#include <stdio.h>
/* random record description - could be anything */
struct rec
{
int x,y,z;
};
/* writes and then reads 10 arbitrary records
from the file "junk". */
int main()
{
int i,j;
FILE *f;
struct rec r;
/* create the file of 10 records */
f=fopen("junk","w");
if (!f)
return 1;
for (i=1;i<=10; i++)
{
r.x=i;
fwrite(&r,sizeof(struct rec),1,f);
}
fclose(f);
/* read the 10 records */
f=fopen("junk","r");
if (!f)
return 1;
for (i=1;i<=10; i++)
{
fread(&r,sizeof(struct rec),1,f);
printf("%d\n",r.x);
}
fclose(f);
return 0;
}
The bit I'm confused about is the bit where the program writes a number to the structure then to the file
Code:
for (i=1;i<=10; i++)
{
r.x=i;
fwrite(&r,sizeof(struct rec),1,f);
}
doesn't this mean r.x = the value of i, but once it loops around would r.x equal the new value of i and wipe the old value out of its memory?