Stream Classes
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
- Name file stream classes.
- Name string stream classes.
- Difference ifstream/ofstream?
- What is fstream?
Long Questions
- Explain stream classes with example.
- 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.