Wednesday, 18 March 2015

Program to maintain student details using files Using C


# 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() ;
}


RUN 1 :
~~~~~~~
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

No comments:

Post a Comment