PF - WK 10 - File Handling

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

CS 111

Programming Fundamentals

Fall 2023

Course conducted by
Dr. Mohammad Imran
Assistant Professor
Dept. of Cyber Security
1

Week 10
CS111 – Programming Fundamentals 2

File
▪ File is a mechanism to permanently store data on computer’s
secondary memory (storage)

▪ Remember: Data in the secondary memory is persistent even after


the system is shut down

▪ All system software, application software, documents, images,


videos, configurations are stored as files in the storage devices (hard
disks, USB drives, CDs, etc.)
CS111 – Programming Fundamentals 3

Representation of a file in C++


▪ In C++, files are represented as streams
• Just like input/output streams that we have been using to get data from
or send data to console (keyboard/display)

▪ We open a file for reading and/or writing as a stream

▪ Reading/writing data from/to a file stream is similar to


reading/writing data from/to console

▪ To perform file stream operations, we must include <fstream>


CS111 – Programming Fundamentals 4

Opening a file for reading


▪ To open a file for reading, we declare an object of type ifstream

▪ At the time of declaration, we initialize the iftsream object with


the path of the file to be opened
ifstream inFile ("D:/hello.txt");

The object type is input file stream


CS111 – Programming Fundamentals 5

Opening a file for reading


▪ To open a file for reading, we declare an object of type ifstream

▪ At the time of declaration, we initialize the iftsream object with


the path of the file to be opened
ifstream inFile ("D:/hello.txt");

This is the name of the ifstream


object. You can choose any name but it
should be meaningful…
CS111 – Programming Fundamentals 6

Opening a file for reading


▪ To open a file for reading, we declare an object of type ifstream

▪ At the time of declaration, we initialize the iftsream object with


the path of the file to be opened
ifstream inFile ("D:/hello.txt");

The path and name of the file to be opened, including the extension.
For files in same folder as the program, file name is enough.

▪ To see if the file is successfully opened, we can just check inFile


if (!inFile) // inFile will be NULL (0) if file open failed
cout << "File not found!\n";
CS111 – Programming Fundamentals 7

Reading data from a file


▪ Once a file is successfully opened, we can read data from it in
multiple ways

1. Just use the stream extraction operator >>


string dataFromFile; // string to store data read from the file
inFile >> dataFromFile;// read the data from file to the string
cout << dataFromFile; // print the string

Hello,

▪ But it didn’t print the whole line!!!


• The same problem as with cin…
CS111 – Programming Fundamentals 8

Reading data from a file


▪ Once a file is successfully opened, we can read data from it in
multiple ways

2. Use getline function, as we did to read whole line from keyboard


string dataFromFile; // string to store data read from the file
getline(inFile, dataFromFile);// read one line from file
cout << dataFromFile; // print the string

Hello, world!
CS111 – Programming Fundamentals 9

Reading data from a file


▪ We can also read data character by character using >>
char c; // declare a char variable c
while (!inFile.eof()) //while the End-Of-File character is not seen
{
inFile >> c; // read a character into c
cout << c; // print c
}

Hello, world!

▪ The eof function returns true (1) if end of


file is reached
CS111 – Programming Fundamentals 10

Reading data from a file


▪ Here is another example of using eof()
string dataFromFile; // string to store data read from the file
while (!inFile.eof()) //while the End-Of-File character is not seen
{
getline(inFile, dataFromFile);// read one line from file
cout << dataFromFile << endl; // print the string
}

Hello, world!
Second line…
CS111 – Programming Fundamentals 11

Opening a file for writing


▪ To open a file for writing, we declare an object of type ofstream

▪ At the time of declaration, we initialize the oftsream object with


the path of the file to be opened
ofstream outFile ("D:/hello.txt");

▪ To see if the file is successfully opened, we can just check outFile


if (!outFile)
cout << "File could not be opened!\n";
CS111 – Programming Fundamentals 12

Opening a file for writing


▪ What do you think the file hello.txt contains now?

▪ Nothing!

▪ When we open a file for writing, one of two things happen:


1. If the file does not exist, then it is created by the system
2. If the file already exists, then its contents are cleared! It is now
blank!!!
CS111 – Programming Fundamentals 13

Opening a file for writing


▪ If we want to open an existing file for writing without erasing its
data, then we open it for appending
ofstream outFile ("D:/hello.txt", ios::app);

▪ ios::app is a flag which tells the system to open the file for
appending

▪ If the file does not exist, then it is created

▪ If the file exists, then it is opened for writing and its contents remain
CS111 – Programming Fundamentals 14

Writing data to a file


▪ Once a file is successfully created for writing, we can write to it
using stream insertion operator <<
outFile << "We added this line";

▪ If the file was opened without using the ios::app flag, then the line
will simply be added to the top of the file
CS111 – Programming Fundamentals 15

Appending data to a file


▪ Once a file is successfully opened for appending, we can write to it
using stream insertion operator <<
outFile << "We added this line";

▪ If the file was opened with the ios::app flag, then the line will be
added to the end of the file
CS111 – Programming Fundamentals 16

Prepending data to a file


▪ I know what you’re thinking – how to add data to the beginning of
an existing file…

▪ Not allowed!

▪ You will have to:


1. Read all existing data in the old file and save into some array/buffer
2. Write the new data at the beginning of a new file
3. Append all data from the array to the end of the new file
4. Rename new file to old file (you’ll have to delete the old file first…)
CS111 – Programming Fundamentals 17

Modifying data in a file


▪ This is possible if we open a file for reading and writing, but…
▪ … it is easier to:
1. Read the contents of file into a string
2. Search the string for the data to be modified
3. Modify the string
4. Write the string back to the file
CS111 – Programming Fundamentals 18

Some points to note on file handling


▪ You must always check if the file was opened successfully or not
before trying to read from or write to a file

▪ You must always check for end of file using eof() when reading a
file

▪ Whether you open a file for reading, writing or appending, you


must close it before exiting the program
inFile.close();

You might also like