File Handling in C++
Objective
In this chapter learner able to understand:
• File handling in C++
• File Handling classes in C++
• Reading and Writing in file
<fstream> and the File Classes
To perform file I/O, you must include the header <fstream> in your
program.
It defines several classes, including ifstream, ofstream, and fstream.
These classes are derived from istream, ostream, and iostream,
respectively.
istream, ostream, and iostream are derived from ios, so ifstream,
ofstream, and fstream also have access to all operations defined by ios.
Another class used by the file system is filebuf, which provides low-level
facilities to manage a file stream.
Opening and Closing a File
In C++, you open a file by linking it to a stream.
Before you can open a file, you must first obtain a stream. There are three
types of streams: input, output, and input/output.
ifstream in; // input stream to read from file
ofstream out; // output stream to write in file
fstream in_out; // input and output stream to read and write from/to files
Opening and Closing a File
Once you have created a stream, one way to associate it with a file is by using
open() . This function is a member of each of the three stream classes.
Prototype for each is as below:
void ifstream::open(const char *filename, ios::openmode mode = ios::in);
void ofstream::open(const char *filename, ios::openmode mode = ios::out |
ios::trunc);
void fstream::open(const char *filename, ios::openmode mode = ios::in | ios::out);
Opening and Closing a File
You can also check to see if you have successfully opened a file by using the
is_open() function, which is a member of fstream, ifstream, and ofstream.
It has this prototype:
bool is_open( );
if(!out.is_open()) {
cout << "File is not open.\n";
exit(1);
}
To close a file, use the member function close() . For example, to close the file
linked to a stream called out use this statement:
[Link]();
The close() function takes no parameters and returns no value.
File Opening mode parameters C++
ios::app Append to end of file
ios::ate go to end of file on opening
ios::binary Opens file in binary mode
ios::in open file for reading only
ios::out open file for writing only
ios::trunc delete the contents of the file if it exist
Reading and Writing Text Files
Writing in File:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream out("[Link]"); // output stream
if(!out) {
cout << "Cannot open Student file.\n";
return 1;
}
out << "Abhay " << 20 << endl;
out << "Amit " << 24 << endl;
out << "Mukesh " << 23 << endl;
out << "Pankaj " << 22 << endl;
[Link]();
return 0;
}
Reading and Writing Text Files
Reading from File:
ifstream in("[Link]"); // input stream
if(!in) {
cout << "Cannot open Student file.\n";
return 1;
}
string name;
int age;
while(in)
{
in >> name >> age;
if(in)
cout << name << " " << age <<endl;
}
[Link]();
More I/O Functions
put() and get() function:The function put() writes a single character to the
associated stream. Similarly, the function get() reads a single character form
the associated stream.
Prototypes:
istream &get(char &ch);
ostream &put(char ch);
write() and read() function: write() and read() functions write and read
blocks of binary data to/from files.
Prototypes:
istream &read(char *buf, streamsize num);
ostream &write(const char *buf, streamsize num);
Random Access:
istream &seekg(off_type offset, seekdir origin);
ostream &seekp(off_type offset, seekdir origin);
Here origin can be any of ios::beg ,ios::cur or ios::end.