File Handling
File Handling
C++
OOP Basics
Input Device
Program
Output stream
Output device
•Keyboard and screen are default options for i/p and o/p
respectively.
•We can redirect streams to other devices or files also if
required.
File handling in C++
• The data is stored in the devices(hard disk, floppy
disk, CD etc.) using the concept of files. A file is a
collection of related data stored in the particular
area on the disk. Programs can be designed to
perform read and write operations on these files.
data Input
data Output
Output stream
Write data
Classes For File Stream Operations
• The I/O system of C++ contains a set of classes that
define the file handling methods. These classes
include ifstream, ofstream and fstream.
ios
iostream
streambuf
• fstream class:- provides simultaneous input and output
operations. It contains open () with default input mode
and inherits all the functions from istream and ostream
through iostream class.
• ifstream class - provides input operations. Contains
open( ) with default input mode, inherits get (), getline(),
read(), seekg(), close(), tellg() functions through istream
class.
• ofstream class - provides output operations. Contains
open () with default output mode. Inherits functions like
put (), seekp(), tellp(), write() functions through ostream
class
• fstream base: provides operations common to
file stream. Serves as base for fstream ,
ifstream, and ofstream classes. Contains open
and close functons.
e.g.
ifstream fin1; /*ifstrem is File_stream_class
fin1 is a stream_object */
fin1.open (“Country.txt”);
/* This opens a file Country.txt
in input mode i.e. for reading*/
Opening a file in Output mode (Writing)
Syntax:
File_stream_class stream_object;
stream_object . open(“file name”);
e.g.
ofstream fout1; /*ofstrem is File_stream_class
fout1 is a stream_object */
fout1.open (“Country.txt”);
/* This opens a file Country.txt in
output mode i.e. for writing*/
Detecting END-OF-FILE
• Detecting end-of-file is necessary for preventing any
further attempt to read data from the file.
syntax :-
Stream_object . close();
e.g fout1.close();
ofstream fout1, fout2; //Creating a object of ofstream class for writing to the file
fout2.open("Capital.txt");
cout << "Enter the number of countries";
cin >> n;
for (i=0; i < n; i++)
{
cout << "Enter the name of the country ";
cin >> countryarr;
fout1 << countryarr << endl;
cout << "Enter the name of the capital ";
cin >> capitalarr;
fout2 << capitalarr << endl;
}
fout1.close();
fout2.close();
fin1.open("Country.txt"); //Opening the file for
read operations
fin2.open("Capital.txt");
while (fin1&&fin2)
{
if ((fin1.getline(countryarr , size)) ==NULL)
continue;
cout << "Capital of " << countryarr;
fin2.getline(capitalarr , size);
cout << " is " << capitalarr << endl;
}
fin1.close();
fin2.close();
getch();
}
Different modes for opening file
fobj .open(“data.txt”, modes)
1. ios::app append to the end of file
2. ios:: ate go to the EOF
3. ios::binary binary file
4. ios::in open the file in read only
5. ios::nocreate open fails if file does not exists.
6. ios::noreplace open fails if the file already exists
7. ios::out open the file for write only
8. ios::trunc delete contents of fileif it exists.
H E L L O !
I/p ptr
H E L L O !
O/p ptr
O/P prt
Functions for manipulating File pointers
• seekg() : Moves get pointer(input) to a
specified location.
• seekp() : Moves put pointer(output) to a
specified location.
• tellg() : Gives the current position of the get
pointer.
• tellp() : Gives the current position of the put
pointer.
Functions for manipulating File pointers
examples
• fin1.seekg(10);
– This moves the file pointer to the byte number 10 i.e
actually 11th byte as the byte numbers begin from zero.
• ofstream fout1;
fout1.open(“file1.txt”, ios::app);
int p = fout1.tellp();
– On execution of these statements, the output pointer is
moved to the end of the file “file1.txt” and the value of
p will represent the number of bytes in the file.