---
title: "Patient information store and retrieve"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:patient-file-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/patient-file-program
site: BCT Engineering
---

# Patient information store and retrieve

Object Oriented Programming · Chapter 8: Stream Computation for Console and File Input/Output

- Asked in: 2074 Asoj, 5 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/patient-file-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

Write a program to store and retrieve the information of patient (Patient_ID, name, address, age and type) in hospital management system.

## Short answer

To store and retrieve patient info, open patient.dat in binary append mode, write Patient objects using write(), then open for reading and display all records with read(). Fields use char arrays for binary I/O.

## Answer

Assumption: patient data is stored as binary records in the file `patient.dat`; the program adds patients and then retrieves all of them.

```cpp
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

class Patient {
    int id;
    char name[30], address[30], type[20];   // type: e.g. OPD / Emergency
    int age;
public:
    void getData() {
        cout << "Patient ID: ";   cin >> id;
        cout << "Name: ";         cin.ignore(); cin.getline(name, 30);
        cout << "Address: ";      cin.getline(address, 30);
        cout << "Age: ";          cin >> age;
        cout << "Type: ";         cin.ignore(); cin.getline(type, 20);
    }
    void display() {
        cout << id << "\t" << name << "\t" << address << "\t"
             << age << "\t" << type << endl;
    }
};

int main() {
    Patient p;
    int n;
    cout << "How many patients? ";
    cin >> n;

    ofstream fout("patient.dat", ios::binary | ios::app);   // store
    for (int i = 0; i < n; i++) {
        p.getData();
        fout.write((char*)&p, sizeof(p));
    }
    fout.close();

    ifstream fin("patient.dat", ios::binary);                // retrieve
    cout << "\nID\tName\tAddress\tAge\tType\n";
    while (fin.read((char*)&p, sizeof(p)))
        p.display();
    fin.close();
    return 0;
}
```

`write()` and `read()` copy the whole object as bytes, so fixed-size character arrays are used instead of `string`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does the program store a patient object?
   A. Using strcpy only
   B. Using printf
   C. With << text lines
   D. write((char*)&p, sizeof(p)) as bytes
   Answer: D. write() copies the object as raw bytes to the binary file. (Key point: Binary storage)

2. Why are char arrays used instead of string?
   A. write()/read() copy the object bytes, so fixed-size members are needed
   B. cin cannot read string
   C. string is not allowed in C++
   D. char arrays are faster to print
   Answer: A. A string holds a pointer to heap data, so raw byte copying would not save the text. (Key point: Fixed arrays)

3. What ends the retrieval while loop?
   A. fin.close()
   B. fin.read() failing at end of file
   C. A fixed count of 10
   D. The user input
   Answer: B. read() returns false when no full record can be read. (Key point: Retrieval loop)

## Related in Object Oriented Programming

- [Department store billing system with files](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abilling-system-file) — Object Oriented Programming, chapter 8
- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6
- [Student records file with CRN search](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-crn-file-search) — Object Oriented Programming, chapter 8
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3

## Related in other subjects

- [ER diagram: hospital management](https://www.bctengineer.com/question.md?key=semester_6%2Fdatabase_management_system%3Aer-hospital) — Database Management System, chapter 2
- [SQL: customer name and address](https://www.bctengineer.com/question.md?key=semester_6%2Fdatabase_management_system%3Asql-customer-name-address) — Database Management System, chapter 3
- [Project management information system](https://www.bctengineer.com/question.md?key=semester_7%2Fict_project_management%3Apmis) — ICT Project Management, chapter 7
- [Industrial vs information age; information as wealth](https://www.bctengineer.com/question.md?key=semester_7%2Ftechnology_environment_and_society%3Aindustrial-vs-info-age-wealth) — Technology, Environment and Society, chapter 1, asked 2×
