NAME:-Vinay Thakur Reg - No:-11908213 Section:-D1901
NAME:-Vinay Thakur Reg - No:-11908213 Section:-D1901
NAME:-Vinay Thakur Reg - No:-11908213 Section:-D1901
NAME:-Vinay thakur
Reg.no:-11908213
Section:-d1901
Q1.Write a program which will store the student
information
in a file and display the information of only those
students
on screen having age greater than 20.
Ans.
#include <stdio.h>
struct student
{
char name[20];
int age;
} std[3];
int main()
{
int i, num;printf("Enter number of students: ");
scanf("%d", &num);
FILE *fptr, *fptr1;
fptr1 = fopen("E:\\c lab\\clab\\temp.txt","r");
fptr = fopen("E:\\c lab\\Clab\\temp.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
for(i = 0; i < num; ++i)
{
printf("For student%d\nEnter name: ", i+1);
scanf("%s",&std[i].name);
printf("Enter age: ");
scanf("%d", &std[i].age);fwrite(&std,sizeof(std),1,fptr);
}
fclose(fptr);
if(fptr1 == NULL)
{
printf("Error!");
exit(1);
}
while( fread(&std,sizeof(std),1,fptr1)==1)
{
if(std.age>20){
printf ("Age = %d name = %s\n", std.age, std.name);
}
}
fclose(fptr1);
return 0;
}