Stream Classes

Unit 5 Β· Topic 5.3⏱ 8 min read❓ 5 MCQs

Introduction

Stream classes include ifstream, ofstream, fstream, stringstream, istringstream, ostringstream.

Definition

Stream Classes: ifstream, ofstream, fstream, stringstream variants.

C++ Code Example

#include 
#include 
using namespace std;
int main(){
    ofstream out("a.txt"); out<<"hi"; out.close();
    stringstream ss; ss<<"x=5"; 
}

Important Questions

Short Questions

  1. Name file stream classes.
  2. Name string stream classes.
  3. Difference ifstream/ofstream?
  4. What is fstream?

Long Questions

  1. Explain stream classes with example.
  2. Discuss stringstream.

MCQ Practice

1. ifstream is for:

  • Input from file
  • Output to file
  • Both
  • None
Answer: Input from file.

2. ofstream is for:

  • Output to file
  • Input from file
  • Both
  • None
Answer: Output to file.

3. fstream supports:

  • Both input/output
  • Input only
  • Output only
  • None
Answer: Both input/output.

4. stringstream is for:

  • String I/O
  • File I/O
  • Console I/O
  • None
Answer: String I/O.

5. Headers needed:

  • fstream, sstream
  • iostream only
  • stdio.h
  • None
Answer: fstream, sstream.

Quick Revision

  • ifstream input, ofstream output, fstream both.
  • stringstream for strings.