0% found this document useful (0 votes)
31 views4 pages

Record

This C program implements a student record management system that allows users to add, view, search, and delete student records stored in a binary file. It defines a structure to store student data and declares an array of this structure to hold multiple records. Functions are defined to perform each operation on the student records by opening, reading from, and writing to the external file. The main function displays a menu and calls the appropriate function based on the user's selection.

Uploaded by

sthasrijal86
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views4 pages

Record

This C program implements a student record management system that allows users to add, view, search, and delete student records stored in a binary file. It defines a structure to store student data and declares an array of this structure to hold multiple records. Functions are defined to perform each operation on the student records by opening, reading from, and writing to the external file. The main function displays a menu and calls the appropriate function based on the user's selection.

Uploaded by

sthasrijal86
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<stdio.

h>
#include<string.h>

// Structure to represent school records


struct student_record {
char name[100];
char class_name[50];
int roll_number;
} students[100]; // Array to store student records
int n; // Variable to store the number of records

// Function prototypes
void add_records();
void view_records();
void search_records();
void delete_records();

int main() {
int task;

do {
// Display menu options
printf("\n\nMenu:\nAdd student record: 1\nView Records: 2\nSearch Records:
3\nDelete Records: 4\nExit: 5\n\n");

// Prompt user for the chosen task


printf("Which task do you want to perform? (1-5): ");
scanf("%d", &task);

// Perform the selected task based on user input


switch (task) {
case 1:
add_records();
break;
case 2:
view_records();
break;
case 3:
search_records();
break;
case 4:
delete_records();
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Error!!\n");
break;
}

} while (task != 5);

return 0;
}

// Function to add student records to the file


void add_records() {
FILE *fptr;
int i;

// Open the file in write mode


fptr = fopen("school.dat", "w");
if (fptr == NULL) {
printf("File cannot be opened");
return;
}

printf("\n\nSuccessfully opened.\n\n");

// Prompt user for the number of records to enter


printf("How many student records do you want to enter?\n");
scanf("%d", &n);

// Input student information and write to file


for (i = 0; i < n; i++) {
printf("Enter student's name: ");
scanf(" %[^\n]s", students[i].name);
printf("Enter class: ");
scanf(" %[^\n]s", students[i].class_name);
printf("Enter roll number: ");
scanf("%d", &students[i].roll_number);
}

// Write students array to the file


fwrite(&students, sizeof(struct student_record), n, fptr);

// Close the file


fclose(fptr);
}

// Function to view student records from the file


void view_records() {
FILE *fptr;

// Open the file in read mode


fptr = fopen("school.dat", "r");
if (fptr == NULL) {
printf("File cannot be opened");
return;
}

// Reset file position indicator to the beginning


rewind(fptr);

int i;

// Read student records from the file


fread(&students, sizeof(struct student_record), n, fptr);

// Display student information


for (i = 0; i < n; i++) {
printf("\nName: %s\n", students[i].name);
printf("Class: %s\n", students[i].class_name);
printf("Roll Number: %d\n", students[i].roll_number);
}

// Close the file


fclose(fptr);
}

// Function to search and display student records based on name


void search_records() {
FILE *fptr;
char search_name[100];

// Open the file in read mode


fptr = fopen("school.dat", "r");
if (fptr == NULL) {
printf("File cannot be opened");
return;
}

// Prompt user for the name to search


printf("\nEnter name you want to search: ");
scanf(" %[^\n]s", search_name);

// Reset file position indicator to the beginning


rewind(fptr);

int i;

// Read student records from the file and display based on name
fread(&students, sizeof(struct student_record), n, fptr);
for (i = 0; i < n; i++) {
if (strcmp(search_name, students[i].name) == 0) {
printf("\nName: %s\n", students[i].name);
printf("Class: %s\n", students[i].class_name);
printf("Roll Number: %d\n", students[i].roll_number);
}
}

// Close the file


fclose(fptr);
}

// Function to delete student records from the file


void delete_records() {
FILE *fptr, *temp;
int i = 0; // Variable to iterate through student records
// Note: 'n' is used to keep track of the number of records

// Open the file in read mode


fptr = fopen("school.dat", "r");
if (fptr == NULL) {
printf("File cannot be opened");
return;
}

// Open a temporary file in write mode


temp = fopen("temp.dat", "w");
rewind(fptr);

// Read student records from the file, delete unwanted records, and write to
the temporary file
// while (fread(&students, sizeof(struct student_record), 1, fptr) != 0) {
// // Check if the record is not unwanted (you can define your criteria
here)
// if (/* Define your criteria for unwanted records */) {
// // Write the record to the temporary file
// fwrite(&students, sizeof(struct student_record), 1, temp);
// // Decrease the total number of records ('n')
// n--;
// }
// // Move to the next record
// i++;
// }

// Close the files


fclose(fptr);
fclose(temp);

// Remove the original file and rename the temporary file


remove("school.dat");
rename("temp.dat", "school.dat");

printf("Unwanted records have been deleted successfully");


}

You might also like