2080 4

CSC166 ยท 2080 ยท 5 marks

What do you mean by stream? Explain different stream class for file input/output.

What do you mean by stream? Explain different stream class for file input/output. [5]

Object Oriented ProgrammingChapter 8: File handlingStream Class Hierarchy for Console Input/Output

Quick answer

A stream is a sequence of bytes flowing between a program and an input/output device (such as a file, keyboard, or screen). It acts as an abstraction layer between the program and the actual device.

Stream and Stream Classes for File I/O in C++

What is a Stream?

  • We have been using the iostream standard library, which provides cin and cout for reading from standard input and writing to standard output respectively.
  • For file-based I/O, C++ provides another standard library called fstream, which defines specialized stream classes to read from and write to files.

To perform file processing in C++, the header file <fstream> must be included in the source file.


Stream Classes for File Input/Output

The fstream library defines three main data types (stream classes):


1. ifstream (Input File Stream)

  • Represents the input file stream.
  • Used to read information from files.
  • Derived from istream.

Example:

#include <fstream>
using namespace std;

int main() {
    ifstream fin;
    fin.open("data.txt");   // open file for reading
    char ch;
    while (fin >> ch)
        cout << ch;
    fin.close();
    return 0;
}

2. ofstream (Output File Stream)

  • Represents the output file stream.
  • Used to create files and write information to files.
  • Derived from ostream.

Example:

#include <fstream>
using namespace std;

int main() {
    ofstream fout;
    fout.open("data.txt");   // open file for writing
    fout << "Hello, File!";
    fout.close();
    return 0;
}

3. fstream (File Stream)

  • Represents the general file stream.
  • Has the capabilities of both ifstream and ofstream.
  • Can create files, write information to files, and read information from files.

Example:

#include <fstream>
using namespace std;

int main() {
    fstream file;
    file.open("data.txt", ios::in | ios::out);  // read and write
    file << "Writing data";
    file.close();
    return 0;
}

Summary Table

ClassPurposeOperation
ifstreamInput file streamRead from files
ofstreamOutput file streamCreate and write to files
fstreamGeneral file streamRead and write to files

File Pointer Manipulation Functions

The stream classes also support functions to manage file pointer positions:

FunctionDescription
seekg()Moves the get (input) pointer to a specified location
seekp()Moves the put (output) pointer to a specified location
tellg()Returns the current position of the get pointer
tellp()Returns the current position of the put pointer

These functions allow random access within a file, enabling reading or writing at any desired position.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.