ok i got this to work its a sample program in our module but i'm having a problem when i want to seach for the student code it only seeks the file for the entry number not the code can someone help me or walk me through this!!
i want to seek for the code not the entry number its like when i typed the code it would print the whole entry
Code:
#include<stdio.h>
#include<string.h>
typedef struct{
char studno[9];
int age;
char name[30];
}student;
FILE *fil;
void clrstr(char st[])
{
strcpy(st,"\0");
}
main()
{
student cs;
int no,i;
char ch;
no=i=0;
clrscr();
fil=fopen("C:\\Stud.txt","w+b");
puts("enter number of students to process"); scanf("%d",&no);
for(i=1;i<=no;i++)
{
printf("this is student #%d\n",i);
clrstr(cs.studno);
puts("enter student number "); scanf("%s",&cs.studno);
clrstr(cs.name);
puts("enter student name "); scanf("%s",&cs.name);
puts("enter student age "); scanf("%d",&cs.age);
fwrite(&cs,sizeof(cs),1,fil);
}
fclose(fil);
/*reading the contents of file */
clrscr();
fil=fopen("C:\\stud.txt","r+b");
fread(&cs, sizeof(cs), 1, fil);
i=0;
while (!feof(fil))
{
i++;
printf("%d. %s\t%s\t%d\n",i,cs.studno,cs.name,cs.age);
fread(&cs, sizeof(cs), 1,fil);
}
fclose(fil);
ch = getche();
i=0;
/*seeking a record*/
fil = fopen ("C:\\stud.txt","rb+");
puts("enter record number to seek : ");
scanf("%d",&i);
fseek(fil,(i-1)*sizeof(cs),0);
fread(&cs,sizeof(cs),1,fil);
printf("%d. %s\t%s\t%d\n",i,cs.studno,cs.name,cs.age);
fclose(fil);
ch=getche();
}