Program to maintain student details using files
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr ;
int i, n, rollno, s1, s2;
char name[10];
clrscr() ;
fptr = fopen("STUDENT.DAT", "w") ;
printf("Enter the number of students : ");
scanf("%d", &n);
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the roll number : ");
scanf("%d", &rollno);
printf("\nEnter the name : ");
scanf("%s", name);
printf("\nEnter the marks in 2 subjects : ");
scanf("%d %d", &s1, &s2);
fprintf(fptr, "%d %s %d %d \n", rollno, name, s1, s2);
}
fclose(fptr);
fptr = fopen("STUDENT.DAT", "r");
printf("\nRoll No. Name \t\t Sub1 \t Sub2 \t Total\n\n");
for(i = 0; i < n; i++)
{
fscanf(fptr,"%d %s %d %d \n", &rollno, name, &s1, &s2);
printf("%d \t %s \t\t %d \t %d \t %d \n", rollno, name, s1, s2, s1 + s2);
}
fclose(fptr);
getch();
}
Output:
Enter the number of students : 2
Enter the roll number : 101
Enter the name : Udaya
Enter the marks in 2 subjects : 75 80
Enter the roll number : 157
Enter the name : Viji
Enter the marks in 2 subjects : 60 70
Roll No. Name Sub1 Sub2 Total
101 Udaya 75 80 155
157 Viji 60 70 130
-
UpdatedDec 31, 2019
-
Views25,419
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order