Demonstrate a program in C to create a data file named score
Demonstrate a program in C to create a data file named score
#include <stdio.h>
#include <stdlib.h>
struct student
{
int reg_no;
char name[50];
char gender[10];
char address[100];
}s;
int main()
{
FILE *fp;
char another;
fp = fopen("score.dat", "w");
if (fp == NULL) {
printf("Error opening file.\n");
exit(1);
}
do {
printf("Enter student information:\n");
printf("Reg_no: ");
scanf("%d", &s.reg_no);
printf("Name: ");
scanf("%s", s.name);
printf("Gender (M/F): ");
scanf(" %s", s.gender);
printf("Address: ");
scanf("%s", s.address);
fprintf(fp,"%d%s%s",s.reg_no,s.name,s.gender,s.address);
printf("\nAdd another record (Y/N)? ");
scanf(" %c", &another);
} while (another == 'Y' || another == 'y');
fclose(fp);
fp = fopen("score.dat", "r");
if (fp == NULL)
{
printf("Error opening file.\n");
exit(1);
}
printf("\nDisplaying student information:\n");
while (fscanf(fp,"%d%s%s%s",&s.reg_no,s.name,s.gender,s.address)!=EOF)
{
printf("%d\t%s\t%s\t%s",s.reg_no,s.name,s.gender,s.address);
}
fclose(fp);
return 0;
}
Develop a program in C using structure to ask the information of any 12 students with roll_number,
name and marks scored in sub1, sub2, and sub3. Also, display them in proper format along with the
calculation of total and percentage. [Note: the full marks of each subject is 100].
#include <stdio.h>
struct student {
int roll_number;
char name[50];
int sub1;
int sub2;
int sub3;
};
int main() {
struct student s[12];
int i;
return 0;
}
Program example 1) Create a datafile “patient.txt” and store name, disease, age and bed number of a
patient.
#include <stdio.h>
void main()
{
char n[10], d[10];
int a, b;
FILE *fptr;
fptr = fopen(“patient.txt”,”w”);
printf("Enter name, disease, age and bed number");
scanf(“%s %s %d %d”, n, d, &a, &b);
fprintf(fptr,"%s %s %d %d\n”, n, d, a, b);
fclose(fptr);
}