I can't understand this code
The bit I'm confused about is the bit where the program writes a number to the structure then to the fileCode:#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; }
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?Code:for (i=1;i<=10; i++) { r.x=i; fwrite(&r,sizeof(struct rec),1,f); }



LinkBack URL
About LinkBacks


