Trouble reading from .dat file....plz help!

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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?
Last edited on
Please edit your post and put the source inside code tags. It will make your post a lot more legible and folks here will be more likely to look at it.
name and surname in your struct don't appear to have types...

EDIT: Now they do.

-Albatross
Last edited on
Also your "showdetals()" function, Line 10, appears to be a 'void' type, you still need to declare that.
yep.srry.they have types in the programm anyway.I just missed them when i selected the specific part of the programm to copy and paste it.
What does your entry point look like? When you say stops working, do you mean a critical error from your OS?
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? :/
Topic archived. No new replies allowed.