- 2069 Asar · 8 marks
96 students record; pass marks
Short answer
Open "ioe.txt" in write mode; loop 96 times to read name, roll, marks and write each with fprintf. Reopen in read mode; fscanf each record; if marks >=40, print roll and name. Result: Outputs list of students scoring 40 or above.
Write a program in C, to read the following information for 96 students. Student Name, Student roll number, Marks obtained(in 100) Record all data in "ioe.txt" file, and program should print roll number and name of student who have obtained greater than or equal to 40 marks.
Logic.
- Open
ioe.txtin write mode; read name, roll number and marks of 96 students in a loop and write each record withfprintf(). - Close the file, reopen it in read mode.
- Read the records back with
fscanf()until it no longer returns 3, and print the roll number and name of those with marks >= 40.
#include <stdio.h>
#ifndef N
#define N 96
#endif
int main(void)
{
FILE *fp;
char name[30];
int roll;
float marks;
fp = fopen("ioe.txt", "w");
if (fp == NULL) {
printf("Cannot create ioe.txt\n");
return 1;
}
for (int i = 1; i <= N; i++) {
printf("Student %d (name roll marks): ", i);
scanf("%29s %d %f", name, &roll, &marks);
fprintf(fp, "%s %d %.1f\n", name, roll, marks);
}
fclose(fp);
fp = fopen("ioe.txt", "r");
if (fp == NULL) {
printf("Cannot open ioe.txt\n");
return 1;
}
printf("\nStudents with marks >= 40\n%-6s %s\n", "Roll", "Name");
while (fscanf(fp, "%29s %d %f", name, &roll, &marks) == 3)
if (marks >= 40)
printf("%-6d %s\n", roll, name);
fclose(fp);
return 0;
}
Sample run (N reduced to 3 with -DN=3 for the test; the real program uses 96)
Input (entered one after another): Ram 1 65 Sita 2 32 Hari 3 40
Students with marks >= 40
Roll Name
1 Ram
3 Hari
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Pass student array; display passed students · Computer Programming, chapter 7
- Binary file of 48 students; highest mark · Computer Programming, chapter 10
- RECORD.txt item cost with VAT · Computer Programming, chapter 10
- Store 48 students in file with append · Computer Programming, chapter 10
- Read records until end; show nth record · Computer Programming, chapter 10 · asked 2×
- Student records with two functions · Computer Programming, chapter 8
- Student file read/write and modify by roll · Object Oriented Programming, chapter 8
- File stream; student binary file program · Object Oriented Programming, chapter 8
- Normal: IOE marks of 5000 students · Probability and Statistics, chapter 3
- Student file displayed in descending marks order · Object Oriented Programming, chapter 8