Data File Handling
Data File Handling
Chapter-12
Data File Handling
Introduction:
❖ Programs can be designed to perform the read and write operations on these
files.
❖ If bytes flows from device like a keyboard, a disk drive, etc to main memory,
❖ If bytes flow from main memory to devices like a display screen, a printer etc.
fstream.h.
⮚ Stream in C++ :
❖ The I / O system of C++ contains a set of classes that define the file handling
methods.
❖ These classes are derived from fstream base and from the corresponding
iostream.h.
❖ These classes, designed to manage the disk files, are declared in fstream.h
and
therefore
we must
include
this file in
any
program
that uses
files.
Class Meanings
✔ Text Files:
✔ Binary Files:
o In binary files, no delimiters are used for a line and no translations occur
here.
∙ A file must first be opened before data can be read from it or written to it.
∙ In C++ there are two ways to open a file with the file stream object.
∙ The first method is preferred when a single file is used with a stream. However for
managing multiple files with the same stream, the second method is preferred.
∙ In all the three file stream classes, a file can be opened by passing a filename as
the first parameter in the constructor itself.
∙ The syntax for opening a file using constructor is
streamclass_name file_objectname (“filename”)
∙ The syntax of opening a file for output purpose only using an object ofstream
class and the constructor is as follows:
ofstream ofstream_object(“file name”);
Example: ofstream fout (“results.dat”);
∙ The syntax of opening a file for input purpose only using an object ifstream
class and the constructor is as follows:
ifstream ifstream_object(“file name”);
Example: ifstream fin (“results.dat”)
∙ open( ) can be used to open multiple files that use the same stream object.
∙ The syntax for opening a file using open ( ) member function is as follows:
file_stream_class stream_object;
stream_object.open (“file_name”);
∙ 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);
⮚ 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.
∙ Syntax:
stream_object.open(“filename”, mode);
∙ Example:
fout.open(“data”, ios::app) // This opens the file data in the append mode.
ios::ate Open file for updating and move the file ifstream
pointer to the end of file
fout.open(“data”, ios::app) // This opens the file data in the append mode
// 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( );
∙ The data in text files are organized into lines with new line character as
terminator.
∙ Text file need following types of character input and output operations:
✔ put( ) function
✔ get( ) function
✔ put ( ):
∙ The put( ) member function belongs to the class ofstream and writes
ofstream fout(“text.txt”);
fout.put (ch);
∙ fout is the object of ofstream. Text is the name of the file. Value at ch
is written to text.
✔ get( ):
∙ The get( ) member function belong to the class ifstream and reads a
ifstream fin(“text.txt”);
fin.get (ch);
∙ fin is the object of ifstream. Text is the name of the file. Reads a character
✔ getline( ):
∙ 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.
ifstream fin;
∙ Binary files are very much use when we have to deal with database consisting
of records.
∙ The binary format is more accurate for storing the numbers as they are stored
in the exact internal representation.
✔ write ( ):
∙ The write ( ) member function belongs to the class ofstream and which is used to
write binary data to a file.
∙ 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.
∙ 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;
∙ Detecting end of file is necessary for preventing any further attempt to read data
from the file.
∙ It returns a non-zero (true) value if the end of file condition is encountered while
reading; otherwise returns a zero (false).
∙ This is used to execute set statements on reaching the end of the file by the
object fin.
∙ In C++, the file I/O operations are associated with the two file pointers: o Input
pointer (get pointer) o Output pointer (put pointer)
o ifstream, like istream, has a pointer known as get pointer that points to the
element to be read in the next input operation.
o ofstream, like ostream, has a pointer known as put pointer that points to the
location where the next element has to be written.
o Append mode
● When we open a file in read only mode, the input pointer is automatically set at
∙ 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.
To move file pointers to any desired position inside a file, file stream classes
support the following functions.
o seekg() - Moves get file pointer to a specific location
o seekp() - Moves put file pointer to a specific location
o tellg() - Returns the current position of the get pointer
o tellp() - Returns the current position of the put pointer
✔ seekg( ):
∙ Move the get pointer to a specified location from the beginning of a file
o seekg(long);
o seekg(offset, seekdir);
∙ The seekg(long) moves the get pointer to a specified location from the beginning of
a file.
Example: inf.seekg(20);
∙ The offset indicates the number of bytes the get pointer is to be moved from
seekdir position.
Constant Meaning
● Example : Some of the pointer offset calls and their actions are shown in the
following table.
✔ seekp ( ):
∙ Move the put pointer to a specified location from the beginning of a file.
∙ The seekp(long) moves the put pointer to a specified location from the beginning
of a file.
∙ Example: inf.seekp(20);
∙ The offset indicates the number of bytes the put pointer is to be moved from
seekdir position.
object.seekp(m, ios::beg) Move forward by m bytes from the beginning for writing
o Searching
o Appending data
o Deleting a record
o Modifying data