Chapter 8-File Handling
Chapter 8-File Handling
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.
Note: To use the above classes of the fstream library, you must include it in your program as a
header file.
Page | 1
Programming I 2023
Chapter Eight File Handling
8.3. How to Open Files
Before performing any operation on a file, you must first open it. If you need to write to the file,
open it using fstream or ofstream objects. If you only need to read from the file, open it using the
ifstream object.
The three objects, that is, fstream, ofstream, and ifstream, have the open() function defined in
them. The function takes this syntax:
Value Description
ios:: app The Append mode. The output sent to the file is appended to it.
ios::ate It opens the file for the output then moves the read and write control
ios::in It opens the file for a read.
ios::out It opens the file for a write.
ios::trunk If a file exists, the file elements should be truncated prior to its
Example:
#include <iostream.h>
#include <fstream.h>
void main() {
fstream my_file;
my_file.open("my_file", ios::out);
if (!my_file) {
cout << "File not created!";
}
else {
cout << "File created successfully!";
my_file.close();
}
}
However, as a programmer, you should learn to close open files before the program terminates.
The fstream, ofstream, and ifstream objects have the close() function for closing files. The
function takes this syntax:
void close();
Example
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream my_file;
my_file.open("my_file.txt", ios::out);
if (!my_file) {
cout << "File not created!";
}
else {
cout << "File created successfully!";
my_file << "UoG June 2021";
my_file.close();
}
return 0;
}
8.5. How to Read from Files
You can read information from files into your C++ program. This is possible using stream
extraction operator (>>). You use the operator in the same way you use it to read user input from
the keyboard. However, instead of using the cin object, you use the ifstream/ fstream object.
Example:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream my_file;
my_file.open("my_file.txt", ios::in);
if (!my_file) {
cout << "No such file";
}
else {
Page | 3
Programming I 2023
Chapter Eight File Handling
char ch;
while (1) {
my_file >> ch;
if (my_file.eof())
break;
cout << ch;
}
}
my_file.close();
return 0;
}
Note that we also use a while loop together with the getline() function (which belongs to
the ifstream class) to read the file line by line, and to print the content of the file:
Example
// Create a text string, which is used to output the text file
string myText;
// Use a while loop together with the getline() function to read the file line by line
while (getline (MyReadFile, myText)) {
// Output the text from the file
cout << myText;
}
// Close the file
MyReadFile.close();
8.6. Summary
With file handling, the output of a program can be sent and stored in a file.
A number of operations can then be applied to the data while in the file.
A stream is an abstraction that represents a device where input/output operations are performed.
A stream can be represented as either destination or source of characters of indefinite length.
The fstream library provides C++ programmers with methods for file handling.
To use the library, you must include it in your program using the #include preprocessor directive.
Page | 4