Write a C program to create a structure Student containing fields for Roll No., Name, Class, Year and Total Marks. Create 10 students and store them in a file.
#include <stdio.h>
#include <string.h>
// Structure definition
struct Student {
int rollNo;
char name[50];
char class[20];
int year;
float totalMarks;
};
// Function to input student data
void inputStudent(struct Student *s) {
printf("\nEnter Roll No: ");
scanf("%d", &s->rollNo);
getchar(); // Clear newline from buffer
printf("Enter Name: ");
fgets(s->name, sizeof(s->name), stdin);
s->name[strcspn(s->name, "\n")] = 0; // Remove trailing newline
printf("Enter Class: ");
fgets(s->class, sizeof(s->class), stdin);
s->class[strcspn(s->class, "\n")] = 0;
printf("Enter Year: ");
scanf("%d", &s->year);
printf("Enter Total Marks: ");
scanf("%f", &s->totalMarks);
}
// Function to display student data
void displayStudent(struct Student s) {
printf("\n----------------------------------------");
printf("\nRoll No: %d", s.rollNo);
printf("\nName: %s", s.name);
printf("\nClass: %s", s.class);
printf("\nYear: %d", s.year);
printf("\nTotal Marks: %.2f", s.totalMarks);
printf("\n----------------------------------------\n");
}
// Function to write students to file
void writeToFile(struct Student students[], int n, char *filename) {
FILE *fp = fopen(filename, "wb");
if (fp == NULL) {
printf("Error opening file for writing!\n");
return;
}
fwrite(students, sizeof(struct Student), n, fp);
fclose(fp);
printf("\n%d students successfully written to file '%s'\n", n, filename);
}
// Function to read students from file
int readFromFile(struct Student students[], char *filename) {
FILE *fp = fopen(filename, "rb");
if (fp == NULL) {
printf("Error opening file for reading!\n");
return 0;
}
int count = fread(students, sizeof(struct Student), 10, fp);
fclose(fp);
return count;
}
int main() {
struct Student students[10];
char filename[] = "students.dat";
int choice, count;
printf("========== STUDENT RECORD MANAGEMENT ==========\n");
printf("\n1. Enter 10 students and save to file");
printf("\n2. Read and display students from file");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("\n***** ENTERING STUDENT DATA *****\n");
for (int i = 0; i < 10; i++) {
printf("\n--- Student %d ---", i + 1);
inputStudent(&students[i]);
}
writeToFile(students, 10, filename);
printf("\n***** DATA SAVED SUCCESSFULLY *****\n");
printf("\nDo you want to display the entered data? (1=Yes, 0=No): ");
int display;
scanf("%d", &display);
if (display) {
printf("\n***** DISPLAYING STUDENT DATA *****\n");
for (int i = 0; i < 10; i++) {
printf("\nStudent %d:", i + 1);
displayStudent(students[i]);
}
}
break;
case 2:
printf("\n***** READING FROM FILE *****\n");
count = readFromFile(students, filename);
if (count > 0) {
printf("\nSuccessfully read %d students from file '%s'\n", count, filename);
printf("\n***** DISPLAYING STUDENT DATA *****\n");
for (int i = 0; i < count; i++) {
printf("\nStudent %d:", i + 1);
displayStudent(students[i]);
}
}
break;
default:
printf("\nInvalid choice!\n");
}
return 0;
}

Comments
Post a Comment