I try to read a file to which my FILE*
fp points and I want to know where the end of the file is. Therefore I use fseek();
At the end of the file, I want to write data from my structure data
.
void printData(FILE *fp)
{
struct data tmp;
fseek(fp,0,SEEK_END);
while(fread(&tmp,sizeof(struct data),1,fp) > 0)
{
puts("test2");
printf("Vorname: %s\n",tmp.vorname);
printf("Nachname: %s\n",tmp.name);
printf("Adresse: %s\n",tmp.adresse);
}
}
This is how my structure is defined:
struct data
{
char name[30];
char vorname[20];
char adresse[50];
};
My Problem is, that the while loop isn't executed even once. Do I forgot something?
fseek(fp,0,SEEK_END)
positions the file pointer at the end of the file (starting point end of the file offset 0), when you then try to read from the file fread of course doesn't read anything.
instead open the file in append mode and fwrite the number of records, these will be appended to the file.