hello to all I am newbie in c,and this is my first answer on StackOverFlows.com,please be good :),
I really don't understand this behavior.
This is my code:
#include <stdio.h>
typedef struct record{
char nome[20];
enter code herechar cognome[30];
} rec ;
void main(){
rec p={"mario","rossi"};
FILE* f;
f=fopen("x.dat","w");
fwrite(&p,sizeof(rec),2,f);// i write 2 times mario rossi
fclose(f);
f=fopen("x.dat","r");
rec d[2];
fread(d,sizeof(rec),1,f);// pt1
fread(d,sizeof(rec),1,f);
printf("%s %s\n",d[0].nome,d[0].cognome);
printf("%s %s\n",d[1].nome,d[1].cognome);
fclose(f);
f=fopen("x.dat","r");
d[2];
fread(d,sizeof(rec),1,f);// pt2
fread(d+sizeof(rec),sizeof(rec),1,f); //pt3
printf("%s %s\n",d[0].nome,d[0].cognome);
printf("%s %s\n",d[1].nome,d[1].cognome);
fclose(f);
}
I want write and read 2 struct from a file, and redirect them into a array so the questions are:
why the 2 fread
in pt1 line fail to store the struct in array ??
FOR ME IS A MYSTERY: why the same instruction the pt2 line do not fail ?? and the pt3 line fail ???
in the pt3 line I invent the d+sizeof(rec)
because I suppose the pointer arithmetic
i do not know if it is legal, but the output is the same of pt1 line
is a error this code ???
thanks a lot for help me :)
All your reads actually work, but you're not reading what you think you're reading, and you're not storing what you read where you think you're storing it.
The number you pass to fwrite
doesn't say how many times to write the data but how many records the pointer points to.
(That is, the pointer is considered to point to the first element in an array and the number how many elements the array contains.)
Going over your code more or less line by line, skipping over the least interesting bits:
fwrite(&p,sizeof(rec),2,f);
This writes the "mario rossi record" and whatever sizeof(rec)
bytes happens to be stored immediately after it.
That data probably looks like random garbage.
rec d[2];
This array's elements are uninitialised and may contain anything.
Reading its elements is undefined at this stage.
fread(d,sizeof(rec),1,f);// pt1
This reads the "mario rossi record" into the first element of d
.
fread(d,sizeof(rec),1,f);
This reads the "random garbage record" into the first element of d
, replacing the "mario rossi record".
The second element still contains the uninitialised data.
printf("%s %s\n",d[0].nome,d[0].cognome);
This prints the "random garbage record" you wrote earlier.
printf("%s %s\n",d[1].nome,d[1].cognome);
This prints the uninitialised data you never replaced.
d[2];
This line accomplishes nothing whatsoever.
fread(d,sizeof(rec),1,f);// pt2
This reads the "mario rossi record" into the first element of d
.
fread(d+sizeof(rec),sizeof(rec),1,f); //pt3
This reads the "random garbage record" into the second element of d
.
printf("%s %s\n",d[0].nome,d[0].cognome);
This is the only time you output any valid data, which is why it looks like it works.
If you want to write the "mario rossi record" twice, write it twice:
fwrite(&p,sizeof(rec),1,f);
fwrite(&p,sizeof(rec),1,f);