I've created a programm where some1 can choose between entering or printing (on the screen) the name,surname and code of students.These are written in a file i named "test.dat".Up to the point of writing them to the file everything is ok as well as printing them.i've got a problem printing the Last student's name,surname and code.here is my function for printing the details of a student.
#include <stdio.h>
#include <string.h>
struct details
{
char name[12],surname[20];
int code;
};
void showdetails()
{
FILE *p; // in main() there is: struct details m;
p=fopen("test.dat","r+");
printf("Please insert the surname of the student...etc.\n");
char temp[20];
scanf("%s",temp);
rewind(p);
int i=0,found=0;
while(found!=1&&(sizeof(struct details)*i)<=ftell(p))
{
fread(&m,sizeof(struct details)*i,1,p);
if(strcmp(m.surname,temp)==0)
found=1;
i++;
}
if(found==1)
printf("The student with name,%s and surname,%s has code:%d.\n",m.name,m.surname,m.code);
}
So let's say i've got 3 students in the file(name,surname and code).
1.Dave Murray 3456 2.Jack Sparrow 2980 3.Jessica Alba 8976
If i type Murray it will print "The student with name Dave and surname Murray has code:3456".
The same for all others except the last one.what happens is that the programm stops working and i have to close it. can some1 help me/solve this problem?
umm...no.i type for example Alba,and press Enter button.Then it just does nothing.it stops working.No other buttons work..nothing.So my only option is to close it.Is there a mistake in my code? :/