File handling in Object Oriented Programming:
To perform input and output from files, C++ provides the fstream library. It defines several classes including ifstream and ofstream.
We connect stream object to the physical file by creating a stream object by specifying the name of the file as the argument.
#include <fstream>
ofstream outfile(“output”);
ifstream infile(“input”);
Alternatively, the file can be connected by using the open member function with one or more arguments
#include <fstream>
ofstream outfile;
ifstream infile;
infile.open(“input”);
outfile.open(“output”, ios:: app | ios :: nocreate); // do not create a new file, append at the end of file
The file should be disconnected by using close member function. To access and manipulate contents of the file, get, put, read and write methods are provided.
Thanks
Mukesh Rajput
Post A Comment:
0 comments: