Unit 5 - Oop
Unit 5 - Oop
5.1 Introduction:
Classes for File Stream Operations (Introduction)
In C++, file handling is done using **file stream classes**, which allow reading from and writing to files.
These classes are part of the `<fstream>` library and handle files like streams of data, similar to how you
handle console input/output.
These classes make it easy to perform file input/output in C++, similar to how `cin` and `cout` work for
the console.
5.2 Opening and Closing a File
To work with files in C++, you need to **open** a file using file stream classes like `ifstream`, `ofstream`,
or `fstream`. After the file operations are done, the file should be **closed** to free system resources.
Opening a file:
- Files can also be opened directly when creating an object of `ifstream`, `ofstream`, or `fstream`.
Closing a file:
You can detect the end of a file (EOF) using the `.eof()` function, which returns `true` when the end of
the file is reached.
When opening a file, you can specify different modes to control how the file is accessed. These modes
are combined using bitwise OR (`|`).
2. ‘ios::out`: Open for writing (erases existing content if the file exists).
3. `ios::app`: Open for appending (adds data at the end of the file without erasing it).
4. `ios::binary`: Open the file in binary mode.
5. `ios::ate`: Move the file pointer to the end of the file when opening.
Example:
Sequential Input (Reading): Data is read in the order it appears in the file.
Sequential Output (Writing): Data is written to the file in the order it is provided.
File pointers keep track of the current position in the file for reading or writing. You can manipulate
these pointers to move within the file.
- Get the current position of the input(`tellg`) or output (`tellp`) file pointer.
- Move the input (`seekg`) or output (`seekp`) file pointer to a specific position.
- You can move relative to the start (`ios::beg`), current position (`ios::cur`), or end (`ios::end`).
These operations let you control how you navigate through a file, enabling random access rather than
just sequential access.
After opening a file, you can check if it was opened correctly using the `.is_open()` function.
Example:
2. File Stream Error Flags
C++ provides several error flags that can be checked after file operations:
- `fail()`: Returns `true` if an input/output operation fails (like wrong data type).
Example:
If a file stream enters an error state, you can clear it using `.clear()` to reset the error flags and continue
working with the file.
Example:
Example of Complete Error Handling
This way, you can handle errors gracefully and avoid unexpected program crashes.
File Pointers (`seekg()` for input and `seekp()` for output) are used to move to a specific location in the
file.
Command line arguments allow you to pass data to your program when you run it from the terminal or
command prompt. In C++, they are accessed via the `main()` function parameters:
Example Usage:
This will pass `"example.txt"` as the argument `argv[1]` to open and read the file.