#include <stdio.h> #include <stdlib.h> struct student { int symbol_number; char name[50]; float percentage; }; int main() { FILE *fp; int i; // create an array of 10 students struct student students[10]; // prompt the user to enter details for each student printf("Enter details of 10 students:\n"); for (i = 0; i < 10; i++) { printf("Student %d:\n", i+1); printf("Symbol number: "); scanf("%d", &students[i].symbol_number); printf("Name: "); scanf("%s", students[i].name); printf("Percentage: "); scanf("%f", &students[i].percentage); } // write student data to file fp = fopen("students.txt", "w"); if (fp == NULL) { printf("Error opening file.\n"); exit(1); } for (i = 0; i < 10; i++) { fprintf(fp, "%d %s %f\n", students[i].symbol_number, students[i].name, students[i].percentage); } fclose(fp); // read student data from file fp = fopen("students.txt", "r"); if (fp == NULL) { printf("Error opening file.\n"); exit(1); } printf("\nStudent data read from file:\n"); for (i = 0; i < 10; i++) { fscanf(fp, "%d %s %f", &students[i].symbol_number, students[i].name, &students[i].percentage); printf("Symbol number: %d, Name: %s, Percentage: %.2f\n", students[i].symbol_number, students[i].name, students[i].percentage); } fclose(fp); return 0; }
Write a C program to write and read symbol number, name and percentage of any 10 students to/from data file "students.txt".
Getting Info...