Code:
void show(void)
{
FILE *fptr;
long i,j=0;
struct info rec={0,"",""};
if ( (fptr=fopen("file.dat", "rb+"))== NULL )
{
printf("File could not be opened.");
}
else
{
for(k=0; feof(fptr)==0; k++)
{
fseek(fptr, k * (sizeof(struct info)), SEEK_SET);
fread(&rec, sizeof(struct info), 1, fptr);
if (rec.ID!=0)
{
printf("Your ID:%u\nYour name:%s\nYour job:%s\n", rec.ID, rec.name, rec.work);
}
}
fclose(fptr);
}
}
There is a function that adds structures to the file, there is also one that deletes ones, etc...
now I have a problem with displaying all the records (ie. structures) there, so how do I do so?
There is something wrong about the code and I can`t get it!
EDIT: I think SEEK_SET could be replaced with SEEK_CUR, like this:
Code:
for(; feof(fptr)==0; )
{
fseek(fptr, (sizeof(struct info)), SEEK_CUR);
fread(&rec, sizeof(struct info), 1, fptr);
but this doesn`t work also, and I can`t get why!