---
title: "Errors encountered during file operation"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:file-errors"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/file-errors
site: BCT Engineering
---

# Errors encountered during file operation

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

- Asked in: 2069 Chaitra, 5 marks
- Video: [PROGRAMMING USING C++ CLASS-105 (Error Handling during File Operations)](https://www.youtube.com/watch?v=nRXwTiYojUU) — INFO TECH
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/file-errors
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

Explain the different errors encountered during file operation.

## Short answer

Errors include opening nonexistent files, disk full, reading past end, stream not opened, invalid mode, and hardware failure. C++ stream state flags eof(), fail(), bad(), good() test these, and clear() resets them. Always check open success and read/write results.

## Answer

Errors can occur during file operations, for example when opening a missing file or reading past the end. C++ records them in the stream **state flags** of `ios`, which can be tested with member functions.

#### Common error situations
1. Opening a file for reading that does not exist.
2. Opening a file with an invalid or non-existent name/path, or a write-protected file.
3. Opening a file for writing when the disk is full or write permission is missing.
4. Reading past the end of the file.
5. Using a stream that has not been opened, or attempting an operation not allowed by the mode (for example, reading from an `fstream` opened only with `ios::out`).
6. Hardware or disk failures during reading or writing, and unreadable data (reading text where a number is expected).
7. Too many files open at once.

#### Error-state flags and functions
| Function | Meaning |
|---|---|
| `eof()` | True when the end of file is reached (`eofbit`) |
| `fail()` | True when an operation failed, e.g. invalid input or open failure (`failbit`/`badbit`) |
| `bad()` | True on a serious, unrecoverable error (`badbit`) |
| `good()` | True when there is no error (all bits clear) |
| `clear()` | Resets the error flags |

#### Example

```cpp
#include <iostream>
#include <fstream>
using namespace std;
int main() {
    ifstream fin("missing.txt");
    if (fin.fail()) {                       // or: if (!fin)
        cout << "Cannot open file\n";
        return 1;
    }
    int x;
    while (fin >> x) cout << x << endl;
    if (fin.eof()) cout << "End of file reached\n";
    else if (fin.bad()) cout << "Fatal read error\n";
    fin.close();
    return 0;
}
```

Also check `is_open()` after opening, and always test the result of each read or write.

## Check yourself

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

1. Which function becomes true when a read reaches the end of the file?
   A. bad()
   B. good()
   C. eof()
   D. clear()
   Answer: C. eof() reports that the end of file has been reached (eofbit set). (Key point: Reading past end)

2. Which function indicates a serious, unrecoverable stream error?
   A. bad()
   B. eof()
   C. good()
   D. fail() only when input is invalid
   Answer: A. bad() is true when badbit is set, such as a hardware or disk failure. (Key point: bad vs fail)

3. What does clear() do?
   A. Deletes the file contents
   B. Resets the stream's error flags
   C. Closes the file
   D. Moves the pointer to the beginning
   Answer: B. clear() resets the error-state flags so that the stream can be used again. (Key point: Purpose of clear())

4. How can a program detect that opening 'missing.txt' for reading failed?
   A. Wait until eof() becomes true
   B. Check if (fin.fail()) or if (!fin) right after opening
   C. It is impossible to detect
   D. Call fin.clear()
   Answer: B. A failed open sets failbit, so fail() or !fin (and is_open()) can be tested immediately. (Key point: Checking a failed open)

## Related in other subjects

- [File I/O operations, errors, random access](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-io-errors-random-access) — Computer Programming, chapter 10
- [EDM operation principles and errors](https://www.bctengineer.com/question.md?key=semester_3%2Fsurveying_i%3Aedm-operation-and-errors) — Surveying I, chapter 2, asked 2×
- [Face observation errors; optical theodolite](https://www.bctengineer.com/question.md?key=semester_3%2Fsurveying_i%3Aface-observation-errors-optical-theodolite) — Surveying I, chapter 7, asked 2×
- [SUN NFS architecture and operation](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Asun-nfs-architecture) — Distributed System, chapter 2, asked 3×
