---
title: "Write and display 10 employees using file"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:employee-file-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/employee-file-program
site: BCT Engineering
---

# Write and display 10 employees using file

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

- Asked in: 2074 Chaitra, 4 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/employee-file-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

Write a program to write the information of 10 employee in a file. And also display their details in console.

## Short answer

Define an Employee class with id, name, salary and methods to input, write to ofstream, read from ifstream, and display. In main, write 10 records to employee.txt, one per line, then reopen and read/display each record in a loop until end-of-file.

## Answer

The program writes 10 employee records to a text file, then reopens the file and displays them.

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

class Employee {
    int id;
    string name;
    float salary;
public:
    void getData() {
        cout << "Enter id, name, salary: ";
        cin >> id >> name >> salary;
    }
    void writeTo(ofstream &fout) {
        fout << id << " " << name << " " << salary << endl;
    }
    bool readFrom(ifstream &fin) {
        return (fin >> id >> name >> salary) ? true : false;
    }
    void display() {
        cout << id << "\t" << name << "\t" << salary << endl;
    }
};

int main() {
    Employee e;
    ofstream fout("employee.txt");
    for (int i = 1; i <= 10; i++) {
        cout << "Employee " << i << endl;
        e.getData();
        e.writeTo(fout);
    }
    fout.close();

    ifstream fin("employee.txt");
    cout << "\nID\tName\tSalary\n";
    while (e.readFrom(fin))
        e.display();
    fin.close();
    return 0;
}
```

Names are read with `>>`, so they should not contain spaces. The `while` loop stops automatically at end of file.

## Check yourself

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

1. Which stream classes write and read the employee file?
   A. ofstream for writing and ifstream for reading
   B. cout and cin
   C. ifstream for both
   D. ostream and istream only
   Answer: A. ofstream writes to files and ifstream reads from them. (Key point: Classes used)

2. How does the display loop know to stop?
   A. The user types stop
   B. readFrom() returns false at end of file
   C. It counts to 10
   D. The file is deleted
   Answer: B. fin >> fails at end of file, making the while condition false. (Key point: Reading loop)

3. Why should names not contain spaces?
   A. The file refuses spaces
   B. cout cannot print spaces
   C. string cannot hold spaces
   D. They are read with >>, which stops at whitespace
   Answer: D. The extraction operator stops at whitespace. (Key point: Limitation)

## Related in Object Oriented Programming

- [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
- [Manager derived from person and employee](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amanager-person-employee) — 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
- [Random access file transaction processing](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Arandom-access-transaction) — Object Oriented Programming, chapter 8
- [Student file displayed in descending marks order](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-descending) — Object Oriented Programming, chapter 8
- [Student file read/write and modify by roll](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-modify-roll) — Object Oriented Programming, chapter 8

## Related in other subjects

- [Employee text file; sort into binary file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-employee-sorted-binary) — Computer Programming, chapter 10
- [Sort employee structures by salary](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-employee-sort-salary) — Computer Programming, chapter 8, asked 2×
- [Employees with salary above 10,000](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-employee-salary-above-10000) — Computer Programming, chapter 8
- [Display records sorted by name from file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-sorted-records-by-name) — Computer Programming, chapter 10
