cipher
cipher

Reputation: 2484

Using fread() to read multiple records

Following is my code for inserting data into file

Note: I have one structure named record for that

fwrite(&record, sizeof(record),1,fptr);

I use the above code in a loop, to enter multiple records. Now, I have my data reading program as this:

do {
fread(&record, sizeof(record), 1, fptr);
printf("\nName: %s \nAddress: %s \nClass Level: %d \nTelepone: %ld",record.name, record.address, record.classlevel, record.telephone);
}while (feof(fptr));

But it only displays only first record.

I also tried using fseek() for that as follows:

do {
fread(&record, sizeof(record), 1, fptr);
printf("\nName: %s \nAddress: %s \nClass Level: %d \nTelepone: %ld",record.name, record.address, record.classlevel, record.telephone);
fseek(fptr,sizeof(record)+1, SEEK_SET);
}while (feof(fptr));

And Still It didn't work!! I am trying to do an small project on C and This File Handling is just getting on my nerves.

Upvotes: 0

Views: 2508

Answers (1)

jim mcnamara
jim mcnamara

Reputation: 16379

change this:

while (feof(fptr))

to this:

while (!feof(fptr))

Upvotes: 4

Related Questions