Hello friends,
I am newto C and was following a c tutorial to make simple student record to insert ,display, search name ,roll number and marks
here the insert and display function works but search function shows name and roll no but showing marks = 0 eveytime?
here is my code
Code:
#include <stdio.h>
struct student
{
int rollno;
char name[30];
float mark;
}stud;
// FUNCTION TO INSERT RECORDS TO THE FILE
void insert()
{
FILE *fp;
fp =fopen("Record.dat","a");
printf("Enter the roll no: ");
scanf("%d",&stud.rollno);
printf("Enter the name: ");
scanf("\t\t%s",&stud.name);
printf("Enter the mark: ");
scanf("\t%f",&stud.mark);
fwrite(&stud,sizeof(stud),1,fp);
}
/ FUNCTION TO DISPLAY RECORDS
void disp()
{
FILE *fp1;
fp1 =fopen("Record.dat","r");
printf("\nRoll no\t\tName\tMark\n\n");
while(fread(&stud,sizeof(stud),1,fp1))
{
printf("%d\t\t%s\t%.2f\n",stud.rollno,stud.name,stud.mark);
}
}
// FUNCTION TO SEARCH THE GIVEN RECORD
void search()
{
FILE *fp2;
int r,s,avl;
printf("\nEnter the Roll no you want to search: ");
scanf("%d",&r);
avl = avlrollno(r);
if(avl== 0)
{
printf("Roll no %d is not available in the file\n",r);
}
else
{
fp2 = fopen("Record.dat","r");
while(fread(&stud,sizeof(stud),1,fp2))
{
s = stud.rollno;
if(s==r)
{
printf("\nRoll no = %d\n",stud.rollno);
printf("\nName = %s\n",stud.name);
printf("\nMarks = %d\n",stud.mark);
}
}
fclose(fp2);
}
}
// FUNCTION TO CHECK GIVEN ROLL NO IS AVAILABLE //
int avlrollno(int rno)
{
FILE *fp;
int c = 0;
fp =fopen("Record.dat","r");
while(!feof(fp))
{
fread(&stud,sizeof(stud),1,fp);
if(rno==stud.rollno)
{
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
int main()
{
//insert();
search();
//disp();
//update();
return 0;
}