Chapter 12 Data File Handling
Chapter 12 Data File Handling
Chapter 12 Data File Handling
Chapter-12
DATA FILE HANDLING
Introduction:
A file is a collection of related data stored in a particular area on the disk.
Programs can be designed to perform the read and write operations on these files.
In general a file is a sequence of bits, bytes, lines or records whose meaning is defined by its user.
C++ I/O occurs in streams, which are sequence of bytes.
If bytes flows from device like a keyboard, a disk drive, etc to main memory, this is called input
operation.
If bytes flow from main memory to devices like a display screen, a printer etc. this is called output
operation.
In C++, file input/output facilities are implemented through a header file fstream.h.
Stream in C++:
A stream is sequence of bytes. In C++, a stream is a general name given to flow of data.
Different streams are used to represent different kinds of data flow.
The three streams in C++ are as follows.
o Input Stream: The stream that supplies data to the program is known as input stream.
o Output Stream: The stream that receives data from the program is known as output
stream.
o Error Stream: Error streams basically an output stream used by the programs to the file or
on the monitor to report error messages.
Class Meanings
Text Files:
o A text file is a file that stores the information in ASCII characters.
o Each line of text is terminated by a special character, known as End of Line (EOL) or delimiter.
Binary Files:
o A binary file is a file that contains information in the same format as it is held in memory.
o In binary files, no delimiters are used for a line and no translations occur here.
The syntax of opening a file for output purpose only using an object ofstream class and open( )
member function is as follows:
oftream_object.open(“file name”);
Example: ofstream outfile;
outfile.open (“data”);
outfile.open (“text.dat”);
The syntax of opening a file for input purpose only using an object ifstream class and open( )
member function is as follows:
iftream_object.open(“file name”);
Example: ifstream ifile;
ifile.open (“data”);
To open a file for both input and output, we declare objects of fstream class. We know that the
class fstream is derived from both ifstream and ofstream,
The syntax for opening a file an object of type fstream class and the constructor is as follows:
fstream fstream-object(“file name’, mode);
The syntax for opening a file an object of type fstream class and the open( ) member function is as
follows:
fstream-object.open(“file name’, mode); Important
3 Marks
File Modes:
While using constructors or open( ), the files were created or opened in the default mode.
There was only one argument passed, i.e. the filename.
C++ provides a mechanism of opening a file in different modes in which case the second
parameter must be explicitly passed.
Syntax: stream_object.open(“filename”, mode);
Example: fout.open(“data”, ios::app) // This opens the file data in the append mode.
The lists of file modes are:
Example:
fstreamfout (“text”, ios::out); // open text in output mode
fstream fin(“text”, ios::in); // open text in input mode
fout.open(“data”, ios::app) // This opens the file data in the append mode
fout.open(“data”, ios::app | ios::nocreate)
// This opens the file in the append mode but fails to open if it does not exist
Closing File:
The member function close( ) on its execution removes the linkage between the file and the stream
object.
Syntax: stream_object.close( );
Example: ofstream.close( );
ifstream.close( );
get( ):
The get( ) member function belong to the class ifstream and reads a single character from the
associated stream.
getline( ):
It is used to read a whole line of text. It belongs to the class ifstream.
Syntax: fin.getline(buffer, SIZE)
It reads SIZE characters from the file represented by the object fin or till the new line character is
encountered, whichever comes first into the buffer.
Example:
char book[SIZE];
ifstream fin;
fin.getline (book, SIZE);
write ( ):
The write ( ) member function belongs to the class ofstream and which is used to write binary data
to a file.
Syntax: ofstream_object.write((char *) & variable, sizeof(variable));
These functions take 2 arguments. The first is the address of the variable and second the size of the
variable in bytes. The address of the variable must be type casted to pointer to character.
Example: student s;
ofstream fout(“std.dat”, ios::binary);
fout.write((char *) &s, sizeof(s));
read ( ):
The read ( ) member function belongs to the class ifstream and which is used to read binary data
from a file.
Syntax: ifstream_object.read((char *) & variable, sizeof(variable));
These functions take 2 arguments. The first is the address of the variable and second the size of the
variable in bytes. The address of the variable must be type casted to pointer to character.
Example: student s;
ifstream fin(“std.dat”, ios::binary)
fin.write((char *) &s, sizeof(s));
When we open a file in read only mode, the input pointer is automatically set at the beginning so
that we read the file from the beginning.
When we open a file in write only mode, the existing contents are deleted and output pointer is set
at the beginning
If we want to open an existing file to add more data, the file is opened in append mode. This
moves the file pointer to the end of the file.
seekg( ):
Move the get pointer to a specified location from the beginning of a file.
There are two types:
o seekg(long);
o seekg(offset, seekdir);
The seekg(long) moves the get pointer to a specified location from the beginning of a file.
8|Page Keerthi Kumar H M
Chapter 12- Data File Handling II PUC, MDRPUC, Hassan
Example: inf.seekg(20);
Constant Meaning
ios::beg seek from beginning of file
ios::cur seek from current location
ios::end seek from end of file
seekp ( ):
Move the put pointer to a specified location from the beginning of a file.
There are two types:
o seekp(long);
o seekp(offset, seekdir);
The seekp(long) moves the put pointer to a specified location from the beginning of a file.
Example: inf.seekp(20);
object.seekp(m, ios::beg) Move forward by m bytes from the beginning for writing
object.seekp(-m, ios::end) Go backward by m bytes from the end for writing
Important Questions
2 Marks Question:
1. Differentiate between ifstream and ofstream. [March 2015]
2. What is a stream? Mention any one stream used in C++. [June 2015]
3. Write any two member functions belonging to of stream class. [March 2016]
4. Write any two member functions belonging to if stream class. [June 2016]
5. Differentiate between read( ) and write( ). [March 2017]
6. Differentiate between put( ) and get( ) functions with reference to binary files. [June 2017]
10 | P a g e Keerthi Kumar H M
Chapter 12- Data File Handling II PUC, MDRPUC, Hassan
3 Marks Question:
1. Give the function of put( ), get( ) and getline( ) with respect to text files. [March 2015]
2. List the fifferent modes of opening a file with their meaning in C++. [June 2015]
3. Give the functions for the following: [March 2016]
a. get( ) b. getline( ) c. read( )
4. Mention the types of data files. Explain. [June 2016]
5. Explain any three modes of to open a file in C++. [March 2017]
6. Write the member function belong to ifstream. [June 2017]
****************
11 | P a g e Keerthi Kumar H M