Originally Posted by
itsme86
BTW, the sizeof operator doesn't work on file sizes. But you don't need it if you use the return value of fread() to control the loop.
itsme86, can you exaplain why this happen:
Code:
#include <stdio.h>
struct student{
int id,age;
char name[30];
};
int main()
{
int i,id2;
struct student record;
FILE *fb;
fb = fopen("data1.txt","wb");
for(i=1; i<=6; i++) {
printf("Enter the student id,name,age");
scanf("%d %s %d",&record.id,record.name,&record.age);
fwrite(&record,sizeof(struct student),1,fb);
}
fclose(fb);
fb=fopen("data1.txt","rb");
for(i=1; i<=sizeof(fb)+1; i++) //while(fread(&record,sizeof(struct student),1,fb))
{
fread(&record,sizeof(struct student),1,fb);
printf("ID %d\tName %s\tage %d\n",record.id,record.name,record.age);
}
fclose(fb);
return 0;
}