4. File Operations
4. File Operations
Chapter 4
Mekonnen K.
(MSc) Email: [email protected]
Topics
Topics Subtopics
2
4: 4.1. Introduction
File 4.2. Stream classes
Operation 4.3. Writing and reading modes
(File I/O) 4.4. Writing to and reading from files
4.5. Types of files (Text and Binary)
4.6. File access methods (sequential and random-
access files)
4.7. Object Oriented Programming Concept
4.7.1 Programming Paradigm
4.7.2. Overview of programming principal
4.7.3. Class and Object
4.7.4. Pillars of Object-Oriented Programming
Concept
4.7.4.1. Encapsulation,
4.7.4.2. Abstraction,
4.7.4.3. Inheritance
4.7.4.5. Polymorphism
Introduction to File
o A file is a collection of related data stored in a
particular area on the disk . The data is stored in disk
using the concept of file .
ifstream fsIn;
ofstream fsOut;
fsIn.open("prog1.txt");
fsOut.open("prog2.txt");
fsIn.close();
fsOut.close();
return 0;
}
Opening a Files
A file must first be opened before data can be read from
it or written to it.
Opening a file associates a file stream variable
declared in the program with a physical file at the
source, such as a disk.
In C++ there are two ways to open a file with the file
stream object.
o Opening file using constructor.
o Opening file using open ( ) member function
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.
Opening Files using Constructors
In order to access a file, it has to be opened either in read,
write or append mode.
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”);
Opening Files using Constructors (1)
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”);
Opening Files using open( )
open( ) can be used to open multiple files that use the same
stream object.
stream_object.open (“file_name”);
ofstream outfile;
outfile.open (“data”);
outfile.open (“text.dat”);
ifile.open (“data”);
Opening Files using open( ) (2)
To open a file for both input and output , we declare objects
of fstream class fstream is derived from both ifstream and
ofstream.
fout.open(“data”, ios::app) // This opens the file data in the append mode
If ( ! Mystream) If ( ! Mystream.is_open())
{ {
cout << “Cannot open file.\n ”; cout << “File is not open.\n ”;
} }
File I/O Example: Open the file with validation
First Method (use the constructor) Second Method ( use Open function)
Syntax: stream_object.close( );
Example: ofstream.close( );
ifstream.close( );
Input and output operation in text file
The data in text files are organized into lines with new line
character as terminator.
operations:
o put( ) function
o get( ) function
Input and output operation in text file (1)
put ( )
Constant Meaning
ios::beg seek from beginning of file
ios::cur seek from current location
ios::end seek from end of file
o Syntax:
o stream_objectname.seekg(offset, origin_value);
Functions for manipulation of file pointers (3)
o Example : Some of the pointer offset calls and their actions
are shown in the following table
o seekp(offset, seekdir);
o Appending data
o Deleting a record
o Modifying data
Types of Files
The C++ language supports two types of files:
• Text files
• Binary files
A text file is a file that stores the information in ASCII characters.
Each line of text is terminated by a special character, known as End of
Line (EOL) or delimiter.
A binary file is a file that contains information in the same format as it
is held in memory.
In binary files, no delimiters are used for a line and no translations occur
here.
Types of Files (1)
A text file stores data in the form of alphabets, digits and
other special symbols by storing their ASCII values and are in
a human readable format. For example, any file with a .txt, .c,
etc extension.
Whereas, a binary file contains a sequence or a collection of
bytes which are not in a human readable format.
For example, files with .exe, .mp3, etc extension. It
represents custom data.
A small error in a textual file can be recognized and
eliminated when seen. Whereas, a small error in a binary
file corrupts the file and is not easy to detect.
Types of Files (2)
The basic difference between text files and binary files
is that in text files various character translations are
performed such as “\r+\f” is converted into “\n”,
whereas in binary files no such translations are
performed.
Create an Object
}; myCar.model;
return 0;}
C++ Polymorphism