PF-Lab-16-File Handling
PF-Lab-16-File Handling
PF-Lab-16-File Handling
File Handling
What is file handling in C++?
Files store data permanently in a storage device. With file handling, the output from a program can be
stored in a file. Various operations can be performed on the data while in the file. A stream is an
abstraction of a device where input/output operations are performed. You can represent a stream as either
a destination or a source of characters of indefinite length. This will be determined by their usage. C++
provides you with a library that comes with methods for file handling.
To use the above classes of the fstream library, you must include it in your program as a header file.
int main() {
// Specify the file path
const std::string filePath = "output.txt";
#include <iostream>
#include <fstream>
#include <string>
int main() {
// Specify the file path
const std::string filePath = "input.txt";
Problem No: 1
Write a C++ program, which initializes a string variable to the content "Time is a great teacher but
unfortunately it kills all its pupils. Berlioz" and write the string to a file OUT.txt. Then read the content
from the text file OUT.txt, count and display the number of alphabets present in it.
Problem No: 2
Write a C++ program to create a file “story.txt”. Write the following lines into the file using fstream.
“The rose is red.
A girl is playing there.
There is a playground.
An aeroplane is in the sky.
Numbers are not allowed in the password.”
Now copy the contents of this file into another file named “output.txt”
Submission Instructions:
1. Save all .cpp files with your lab no, roll no and task number e.g. LAB14_i23XXXX_Problem01.cpp
2. Take the screenshots of your program outputs showing on the terminal.
3. Submit all .cpp files directly on Google Classroom.