HOSPITAL MANAGEMENT SYSTEM USING FILES
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 100
#define MAX_GENDER_LEN 10
#define DIAGNOSIS_LEN 200
// Structure for appointment information
struct Appointment {
char doctorName[MAX_NAME_LEN];
char date[20]; // Date format "dd-mm-yyyy"
char time[10]; // Time format "hh:mm"
struct Appointment *next;
};
// Structure for patient information
struct Patient {
int patientID;
char name[MAX_NAME_LEN];
int age;
char gender[MAX_GENDER_LEN];
char diagnosis[DIAGNOSIS_LEN];
float totalBill;
struct Appointment *appointments; // Linked list for appointments
struct Patient *next;
};
// File pointer for storing patient data
FILE *hospitalFile = NULL;
// Function to create a new appointment node
struct Appointment* createAppointment(char* doctorName, char* date, char* time) {
struct Appointment *newAppointment = (struct Appointment*) malloc(sizeof(struct
Appointment));
strcpy(newAppointment->doctorName, doctorName);
strcpy(newAppointment->date, date);
strcpy(newAppointment->time, time);
newAppointment->next = NULL;
return newAppointment;
// Function to create a new patient node
struct Patient* createPatient(int patientID, char* name, int age, char* gender, char* diagnosis) {
struct Patient *newPatient = (struct Patient*) malloc(sizeof(struct Patient));
newPatient->patientID = patientID;
strcpy(newPatient->name, name);
newPatient->age = age;
strcpy(newPatient->gender, gender);
strcpy(newPatient->diagnosis, diagnosis);
newPatient->totalBill = 0; // Initially set total bill to 0
newPatient->appointments = NULL; // No appointments initially
newPatient->next = NULL;
return newPatient;
// Function to open the file
void openFile(char *filename) {
hospitalFile = fopen(filename, "r+b");
if (hospitalFile == NULL) {
printf("File not found. Trying to create file...\n");
hospitalFile = fopen(filename, "w+b"); // Create the file if it does not exist
if (hospitalFile == NULL) {
printf("Error opening or creating file.\n");
exit(1);
printf("File created successfully.\n");
// Function to close the file
void closeFile() {
if (hospitalFile != NULL) {
fclose(hospitalFile);
// Function to write patient to the file
void writePatientToFile(struct Patient* patient) {
if (hospitalFile != NULL) {
fseek(hospitalFile, 0, SEEK_END); // Move to the end of the file before writing
fwrite(patient, sizeof(struct Patient), 1, hospitalFile);
// Write the appointments
struct Appointment* appointment = patient->appointments;
while (appointment != NULL) {
fwrite(appointment, sizeof(struct Appointment), 1, hospitalFile);
appointment = appointment->next;
}
// Write a null appointment marker to indicate the end of the list
struct Appointment nullAppointment = {0};
fwrite(&nullAppointment, sizeof(struct Appointment), 1, hospitalFile);
// Function to read patients from the file
void readPatientsFromFile() {
if (hospitalFile == NULL) {
printf("No data to read.\n");
return;
struct Patient patient;
fseek(hospitalFile, 0, SEEK_SET); // Go to the beginning of the file
while (fread(&patient, sizeof(struct Patient), 1, hospitalFile)) {
printf("ID: %d, Name: %s, Age: %d, Gender: %s, Diagnosis: %s, Total Bill: %.2f\n",
patient.patientID, patient.name, patient.age, patient.gender, patient.diagnosis,
patient.totalBill);
// Read appointments for the patient
struct Appointment* temp = NULL;
struct Appointment* last = NULL;
while (1) {
struct Appointment appointment;
fread(&appointment, sizeof(struct Appointment), 1, hospitalFile);
if (appointment.doctorName[0] == 0) { // Check for the null appointment marker
break;
struct Appointment* newAppointment = (struct Appointment*)malloc(sizeof(struct
Appointment));
*newAppointment = appointment;
newAppointment->next = NULL;
if (temp == NULL) {
temp = newAppointment;
} else {
last->next = newAppointment;
last = newAppointment;
// Print appointments
struct Appointment* app = temp;
while (app != NULL) {
printf(" Appointment: Doctor: %s, Date: %s, Time: %s\n", app->doctorName, app->date, app-
>time);
app = app->next;
// Function to generate a bill for a patient
void generateBill(int patientID) {
struct Patient patient;
int found = 0;
fseek(hospitalFile, 0, SEEK_SET); // Go to the beginning of the file
while (fread(&patient, sizeof(struct Patient), 1, hospitalFile)) {
if (patient.patientID == patientID) {
found = 1;
float consultationFee, treatmentFee, medicationFee, roomCharges;
printf("\nGenerating bill for patient: %s (ID: %d)\n", patient.name, patient.patientID);
// Ask for various fees
printf("Enter consultation fee: ");
scanf("%f", &consultationFee);
printf("Enter treatment fee: ");
scanf("%f", &treatmentFee);
printf("Enter medication fee: ");
scanf("%f", &medicationFee);
printf("Enter room charges: ");
scanf("%f", &roomCharges);
// Calculate the total bill
patient.totalBill = consultationFee + treatmentFee + medicationFee + roomCharges;
// Update the file with the new bill
fseek(hospitalFile, -sizeof(struct Patient), SEEK_CUR); // Move the pointer back to overwrite
the patient
fwrite(&patient, sizeof(struct Patient), 1, hospitalFile);
// Display the bill
printf("\n--- Bill Summary ---\n");
printf("Consultation Fee: %.2f\n", consultationFee);
printf("Treatment Fee: %.2f\n", treatmentFee);
printf("Medication Fee: %.2f\n", medicationFee);
printf("Room Charges: %.2f\n", roomCharges);
printf("Total Bill: %.2f\n", patient.totalBill);
return;
}
}
if (!found) {
printf("Patient with ID %d not found.\n", patientID);
// Function to add a patient
void addPatient(int patientID, char* name, int age, char* gender, char* diagnosis) {
struct Patient *newPatient = createPatient(patientID, name, age, gender, diagnosis);
writePatientToFile(newPatient);
printf("Patient added successfully.\n");
// Function to schedule an appointment for a patient
void scheduleAppointment(int patientID) {
struct Patient patient;
int found = 0;
fseek(hospitalFile, 0, SEEK_SET); // Go to the beginning of the file
while (fread(&patient, sizeof(struct Patient), 1, hospitalFile)) {
if (patient.patientID == patientID) {
found = 1;
char doctorName[MAX_NAME_LEN], date[20], time[10];
// Get appointment details
printf("Enter doctor's name: ");
getchar(); // To consume newline left by previous input
fgets(doctorName, sizeof(doctorName), stdin);
doctorName[strcspn(doctorName, "\n")] = 0; // Remove newline
printf("Enter appointment date (dd-mm-yyyy): ");
fgets(date, sizeof(date), stdin);
date[strcspn(date, "\n")] = 0; // Remove newline
printf("Enter appointment time (hh:mm): ");
fgets(time, sizeof(time), stdin);
time[strcspn(time, "\n")] = 0; // Remove newline
// Create new appointment and link it to the patient
struct Appointment* newAppointment = createAppointment(doctorName, date, time);
newAppointment->next = patient.appointments;
patient.appointments = newAppointment;
// Write updated patient data to the file
fseek(hospitalFile, -sizeof(struct Patient), SEEK_CUR); // Move the pointer back to overwrite
the patient
fwrite(&patient, sizeof(struct Patient), 1, hospitalFile);
printf("Appointment scheduled successfully.\n");
return;
if (!found) {
printf("Patient with ID %d not found.\n", patientID);
int main() {
int choice;
int patientID;
char name[MAX_NAME_LEN], gender[MAX_GENDER_LEN], diagnosis[DIAGNOSIS_LEN];
int age;
// Open the hospital data file
openFile("patients.dat");
while (1) {
printf("\nHospital Management System\n");
printf("1. Add Patient\n");
printf("2. Display Patients\n");
printf("3. Generate Bill for Patient\n");
printf("4. Schedule Appointment\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// Add patient
printf("Enter Patient ID: ");
scanf("%d", &patientID);
getchar(); // To consume newline left by previous input
printf("Enter Patient Name: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = 0; // Remove newline
printf("Enter Age: ");
scanf("%d", &age);
getchar(); // To consume newline left by previous input
printf("Enter Gender: ");
fgets(gender, sizeof(gender), stdin);
gender[strcspn(gender, "\n")] = 0; // Remove newline
printf("Enter Diagnosis: ");
fgets(diagnosis, sizeof(diagnosis), stdin);
diagnosis[strcspn(diagnosis, "\n")] = 0; // Remove newline
addPatient(patientID, name, age, gender, diagnosis);
break;
case 2:
// Display patients and their appointments
readPatientsFromFile();
break;
case 3:
// Generate bill for a patient
printf("Enter Patient ID to generate bill: ");
scanf("%d", &patientID);
generateBill(patientID);
break;
case 4:
// Schedule an appointment
printf("Enter Patient ID to schedule appointment: ");
scanf("%d", &patientID);
scheduleAppointment(patientID);
break;
case 5:
// Exit system
closeFile();
printf("Exiting the system...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
return 0;
OUTPUT
C:\Users\91994\OneDrive\Desktop\virtual_mouse>gcc hospital_management.c -o
hospital_management
C:\Users\91994\OneDrive\Desktop\virtual_mouse>hospital_management reservations.dat
Hospital Management System
1. Add Patient
2. Display Patients
3. Generate Bill for Patient
4. Schedule Appointment
5. Exit
Enter your choice: 1
Enter Patient ID: 1
Enter Patient Name: arun
Enter Age: 18
Enter Gender: male
Enter Diagnosis: fever
Patient added successfully.
Hospital Management System
1. Add Patient
2. Display Patients
3. Generate Bill for Patient
4. Schedule Appointment
5. Exit
Enter your choice: 2
ID: 1, Name: arun, Age: 18, Gender: male, Diagnosis: fever, Total Bill: 0.00
ID: 1, Name: arun, Age: 18, Gender: male, Diagnosis: fever, Total Bill: 0.00
Hospital Management System
1. Add Patient
2. Display Patients
3. Generate Bill for Patient
4. Schedule Appointment
5. Exit
Enter your choice: 3
Enter Patient ID to generate bill: 1
Generating bill for patient: arun (ID: 1)
Enter consultation fee: 1000
Enter treatment fee: 100000
Enter medication fee: 100000
Enter room charges: 10000
--- Bill Summary ---
Consultation Fee: 1000.00
Treatment Fee: 100000.00
Medication Fee: 100000.00
Room Charges: 10000.00
Total Bill: 211000.00
Hospital Management System
1. Add Patient
2. Display Patients
3. Generate Bill for Patient
4. Schedule Appointment
5. Exit
Enter your choice: 4
Enter Patient ID to schedule appointment: 1
Enter doctor's name: kumar
Enter appointment date (dd-mm-yyyy): 22-2-22
Enter appointment time (hh:mm): 11-22
Appointment scheduled successfully.
Hospital Management System
1. Add Patient
2. Display Patients
3. Generate Bill for Patient
4. Schedule Appointment
5. Exit
Enter your choice: 5
Exiting the system...
C:\Users\91994\OneDrive\Desktop\virtual_mouse>
Simple Algorithm for Hospital Management System
1. Add Patient
1. Input patient details (ID, Name, Age, Gender, Diagnosis).
2. Store the details in the file.
3. Confirm patient has been added.
2. Display Patients
1. Read all patients from the file.
2. Display each patient's ID, Name, Age, Gender, and Diagnosis.
3. Generate Bill
1. Input the patient ID for which the bill is to be generated.
2. Search for the patient by ID.
3. Input the various charges (consultation, treatment, medication, room).
4. Calculate the total bill and update the file with the total bill.
5. Display the bill.
4. Schedule Appointment
1. Input the patient ID for scheduling the appointment.
2. Input appointment details (Doctor's Name, Date, Time).
3. Store the appointment in the file (linked to the patient's record).
4. Confirm appointment is scheduled.
5. Display Appointments
1. Input the patient ID to view appointments.
2. Display all scheduled appointments for that patient.
6. Exit
1. Exit the program after closing any open files.
PSEUDOCODE
BEGIN
OPEN file "hospital_data.dat" for reading and writing
LOOP until user selects exit:
Display menu with options:
1. Add Patient
2. Display Patients
3. Generate Bill
4. Schedule Appointment
5. Display Appointments
6. Exit
INPUT choice from user
IF choice == 1 THEN
PROMPT user for Patient details (ID, Name, Age, Gender, Diagnosis)
ADD patient to file
ELSE IF choice == 2 THEN
OPEN the file
DISPLAY all patients from file
ELSE IF choice == 3 THEN
PROMPT user for Patient ID
SEARCH for patient in file
IF patient found THEN
PROMPT for bill details (consultation, treatment, medication, room)
CALCULATE the total bill
UPDATE total bill in file
DISPLAY the bill
ELSE IF choice == 4 THEN
PROMPT user for Patient ID and appointment details (Doctor's Name, Date, Time)
ADD appointment to file
ELSE IF choice == 5 THEN
PROMPT user for Patient ID
DISPLAY all appointments for the patient
ELSE IF choice == 6 THEN
CLOSE file
EXIT program
ELSE
PRINT "Invalid choice. Try again."
END LOOP
END
Flowchart Structure
1. Start (Oval)
o Indicating the start of the program.
2. Display Menu (Rectangle)
o Show options:
Add Patient
Display Patients
Generate Bill
Schedule Appointment
Display Appointments
Exit
3. Input User Choice (Parallelogram)
o Get user input for the desired option.
4. Decision (Diamond)
o Check which option the user has selected (Add Patient, Display Patients, Generate
Bill, etc.).
5. Add Patient (Rectangle)
o If the user chooses "Add Patient", input the patient's details and save to the file.
6. Display Patients (Rectangle)
o If the user chooses "Display Patients", read and display the patients' data from the
file.
7. Generate Bill (Rectangle)
o If the user chooses "Generate Bill", input charges, calculate total, and update the file
with the bill.
8. Schedule Appointment (Rectangle)
o If the user chooses "Schedule Appointment", input the appointment details and save
them.
9. Display Appointments (Rectangle)
o If the user chooses "Display Appointments", show the appointments for a selected
patient.
10. Exit (Oval)
If the user selects "Exit", close the file and end the program.
Flow Direction
Use arrows between these shapes to show the direction of flow. Each decision leads to the
next action or process based on user input.