0% found this document useful (0 votes)
23 views

Demonstrate a program in C to create a data file named score

The document contains C programs that demonstrate how to create data files for storing student and patient information. The first program collects and stores student details such as registration number, name, gender, and address in a file named 'score.dat', and displays the records afterward. The second program collects data for 12 students including roll number, name, and marks in three subjects, calculates total and percentage, and displays the results in a formatted manner.

Uploaded by

Emick Ghimire
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Demonstrate a program in C to create a data file named score

The document contains C programs that demonstrate how to create data files for storing student and patient information. The first program collects and stores student details such as registration number, name, gender, and address in a file named 'score.dat', and displays the records afterward. The second program collects data for 12 students including roll number, name, and marks in three subjects, calculates total and percentage, and displays the results in a formatted manner.

Uploaded by

Emick Ghimire
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Demonstrate a program in C to create a data file named score.

dat to store students’ information with


Reg_no, name, gender, and address. The program should ask the user to continue or not. When
finished, the program should also display all the records in the proper format.

#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;

// input student data


for (i = 0; i < 12; i++) {
printf("Enter Roll Number, Name, Sub1 marks, Sub2 marks, Sub3 marks of student %d:\n", i+1);
scanf("%d %s %d %d %d", &s[i].roll_number, s[i].name, &s[i].sub1, &s[i].sub2, &s[i].sub3);
}

// print student data


printf("\nRoll No.\tName\t\tSub1\tSub2\tSub3\tTotal\tPercentage\n");
for (i = 0; i < 12; i++) {
int total = s[i].sub1 + s[i].sub2 + s[i].sub3;
float percentage = (float)total / 3;
printf("%d\t\t%s\t\t%d\t%d\t%d\t%d\t%.2f%%\n", s[i].roll_number, s[i].name, s[i].sub1, s[i].sub2,
s[i].sub3, total, percentage);
}

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

You might also like