C++PPT (File Stream Classes) - 096 & 073
C++PPT (File Stream Classes) - 096 & 073
CLASSES
By : Aman Rawal 07317702021
Gurjyot Singh 09617702021
TABLE OF CONTENTS
01 03
Definition of File Example of File
Stream Stream Classes
02 04
Types of MCQ and
File Stream Questions
Classes
01
DEFINITION OF FILE STREAM CLASSES
DEFINITION
File streams in C++ are basically the libraries
that are used in the due course of
programming. The programmers generally use
the iostream standard library in the C++
programming as it provides the cin and cout
methods that are used for reading from the
input and writing to the output respectively.
02
TYPES OF FILE STREAM CLASSES
TYPES OF FILE
STREAM CLASSES
There are three types of file stream classes in C+
+:
ofstream: output file stream class, used for
writing to files.
ifstream: input file stream class, used for
reading from files.
fstream: file stream class, used for both
reading and writing to files.
ifstream ( Input File Stream )
You can use an ifstream object to open a file for reading and then use
the stream extraction operator (>>).
You can use an ofstream object to open a file for writing and then use
the stream insertion operator (<<).
You can use an fstream object to open a file for reading and writing,
and then use the stream insertion operator (<<) or the stream
extraction operator (>>) to read from or write to the file.
How to use file stream classes
To use a file stream class, you first need to include the <fstream>
header.
Then, you can create an object of the appropriate file stream class
(e.g., ofstream, ifstream, or fstream) and open a file using the open
function.
Once the file is open, you can use the stream insertion (<<) or
stream extraction (>>) operators to read from or write to the file
03
EXAMPLE OF FILE STREAM CLASSES
EXAMPLE OF “ifstream”
#include <fstream>
#include <iostream>
int main(){
std::ifstream in("file.txt");
std::string line;
while (std::getline(in, line)) {
std::cout << line << std::endl;
}
in.close();
return (0);
}
OUTPUT
file.txt Output
Hello There! Hello There!
Hello World!
EXAMPLE OF “ofstream”
#include <fstream>
#include <iostream>
int main()
{
std::ofstream out("file.txt");
out << "Hello, world!" << std::endl;
out.close();
}
OUTPUT
file.txt Output
Hello World!
Hello World!
EXAMPLE OF “fstream”
#include <fstream>
#include <iostream>
int main(){
std::fstream io("file.txt",
std::ios::in | std::ios::out);
io << "Goodbye, world!" << std::endl;
io.seekg(0);
while (std::getline(io, line)) {
std::cout << line << std::endl;
}
io.close();
}
OUTPUT
file.txt Output
Goodbye, world! Goodbye, world!
Hello World!
04
MCQ AND QUESTIONS
MCQs
a) <ifstream> c) <fstream>
b) <ostream> d) <iostream> Ans : c) fstream
a) Ofstream c) iostream
b) Ifstream d) fstream Ans : a) Ofstream
Q3. Which of the following is used to create a stream that
performs both input and output operations?
1. What are the steps involved in using a file in C++? (2019) 5 marks
3. Give the syntax of write() and read() functions using in file streams
(2018) 4 marks