0% found this document useful (0 votes)
34 views32 pages

Unit 05 File Operation

This document covers file operations in C++, including stream classes for managing file input and output, detection of end-of-file, and various file modes. It explains how to open and close files using constructors and the open() function, as well as formatted input/output functions and access methods (sequential and random). Additionally, it highlights the differences between sequential and random access files.

Uploaded by

vrushalikabade36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views32 pages

Unit 05 File Operation

This document covers file operations in C++, including stream classes for managing file input and output, detection of end-of-file, and various file modes. It explains how to open and close files using constructors and the open() function, as well as formatted input/output functions and access methods (sequential and random). Additionally, it highlights the differences between sequential and random access files.

Uploaded by

vrushalikabade36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Unit No:-05

File Operation

Marks:-10
In this chapter we are going to study that
following points.
5.1 C++ stream classes, classes for file operations.
5.2 Detection of end of file, file modes
5.3 Opening Files:- using constructor and open(),Closing files , Reading from and
writing to files. Formatted input output functions in file.
5.4 Types of file: Random access , Sequential access.
C++ Stream Classes:-
• The c++ I/O system contains a hierarchy of class that are used to define various
stream to deal with both console and disk file.
• These classes are called as stream classes.
• The hierarchy of stream classes are used for input and output operation with the
console unit.
• The classes are declare is header stream.
• This files should be included all the programs that communicate with the console
unit.
• In input output system in c++ provides an interface between the program and
device files this interface known as a stream.
• A stream is a source of sequence of bytes.
• Stream acts as source from which input data can be obtained.
• Stream acts as destination from which output data can be sent.
Input Input Stream
Device

Program
Output Stream
Output
Device
Fig :- Data Streams
Input stream:- Source stream provides data program
known as input stream.

Output Stream:- Destination stream provides that


received output from data program known as output
stream.

The data from input stream comes from keyboard or


other storage devices
The data from output stream can goes on screen or other storage devices
Explain with diagram stream classes for file operations

• The I/O system of C++ contains a set of classes that


defines the file handling methods .
• This include ifstream, ofstream, and fstream.
• These classes are derives from fstreambase and their
the corresponding iostream classes as shown in fig.
• These classes are designed to define manage the disk
files ,are declared in fstream and therefore we must
include this file in any program that uses files.
IOS
Iostream
File
Stream Buffer
Istream Ostream

Iostream
Filebuf

Ifstream fstream Ofstream

Fstream
File
Fstream base
STREAM CLASSES FOR FILE OPEERATION
Stream classes for file operation.

Classes name Contents


IOS • Contains basic facilities that used by
(General Input Output Stream) all other input output classes.
• Also contains pointer to buffer
object(Streambuf object)
• Declares constants and functions that
are necessary for handling formatted
input output operations.

Istream • Inherits the properties of ios


Input Stream • Declares the input function such as
get() , getline() and read().
• Contains overloaded extraction
operator (>>)
Classes name Contents
OStream • Inherits the properties of ios
Output Stream • Declares the output function such as
put() and write().
• Contains overloaded insertion
operator (<<)

IOstream • Inherits the properties of ios istream


Input Output Stream and ostream through multiple
inheritance thus contains all the input
output functions.
Classes name Contents
Streambuf • Provides an interface to physical
devices through buffers.
• Acts as a base for filebuf class ios files

Filebuf • Its purpose is to set the file buffers to


read and write.
• Also contains open and close
member

fstreambase • Provides operations common to file


stream. Serves as a base for fstream,
ifstream, ofstream.
• Also contains open(),close() function.
Classes name Contents

ifstream • Provides input operations.


• Contains open with default input
mode
• Inherits functions get(),getline(),read()
functions from istream

ofstream • . Provides output operations.


• Contains open with default input
mode
• Inherits functions put(),write()
functions from ostream
Classes name Contents
• . Provides support for simultaneous
input and output mode.
fstream • Contains open with default input
mode
• Inherits all the functions from istream
and ostream classes through
iostream.
Detection end-of-file
• When the file read it is necessary to detect end of file.
• The eof() is used to check whether the file pointer is reach at end of file or
not.
• The eof() returns non zero value at the end otherwise it returns zero value.
• Syntax:-if ( file.eof()!=0)
• {
exit 1;}
OR
while(!file.eof())
{
//statements;
}
File Modes:-
• File modes are used to open the file with
specified modes.
• There are different types of file mode
parameters.
• File modes and their parameter are listed below
File Modes Parameters
Parameters Meaning
Ios::app Append to end of file
Ios::ate Go to end-of-file on opening
Ios::binary Binary files
Ios::in Open file for reading only
Ios::nocreate Open fails if the file does not exist
Ios::noreplace Open fails if the file already exists

Ios::out Open file for writing only


Ios::trunc Delete the contents of the file if it
exists
Opening And Closing File:-
• As we are going to perform the operations on a file .
• So there a need to open and close the file, and creating inputstream
and outputstream using (ofstream) (ifstream) stream classes.
• That stream classes define under the header file fstream.
• File can be opening and closing can be done two ways:-
1. OPEN FUNTION ()
2. CONSTRUCTOR()
Opening Files using Open Function()
• The function open()can be used to open the multiples files that use
the same stream object.
• Syntax:-
file-stream-class stream-object;
stream-object.open(“filename”);
Example:-
Ofstream outfile; //Create Stream (for Output)
Outfile.open(“DATA1”); //Connect stream to data1
……….
………
………
Outfile.close(); //Disconnect stream to Data1
Ofstream fout; //create stream (for output)
Fout.open(“DATA2”); // connect stream data2
……….
………..
Fout.close(); // Disconnect data 2

…..
Opening the file using Constructor:-
• We know that a constructor is used to initialize the object of class
while object is created
• Here , a filename is used to initialize the file stream object.
• this involves the following steps.
1. Create a file stream object to manage the stream using the
appropriate classes that is to say ofstream is used to create output
stream and the classes ifstream are used to create input stream.
2. Initialized the object with the desired filename.
Figure:- Two File Streams Working On Seprate Files
Example:-
ofstream outfile (“result”); //file open using constructor.
Output Stream

OutFile Disk
PROGRAM
Results
Data

Input Stream
Data
File
Infile
Example:-
Ofstream outfile (“DATA1”); //Create Stream use constructor (for Output)
//Connect stream to data1
……….
………
………
Outfile.close(); //Disconnect stream to Data1
Ofstream fout (“DATA2”); //create streamUsing Constructor (for output)
// connect stream data2
……….
………..
Fout.close(); // Disconnect data 2

…..
Formatted Input Output
Functions
• C++ supports a number ofIn File
features that could be used for formatting the
output these includes following Features :-

I/O Functions Description


Seekg(offset,refposition); Moves to Get pointer(input) to a
specified location
Seekp(offset,refposition); Moves to Put pointer(output) to a
specified location

Tellg(offset,refposition); Gives the current position of the get


pointer

Offset- number of bytes from file location to move. Refposotion- beg,cur,end


I/O Functions Description
Tellp(offset,refposition); Gives the current position of the
put pointer

Read(char *ch, sizeof()); Used to read the data from


input file

Write(char *ch, sizeof()); Used to write the data from


output file
I/O Functions Description
get(char *ch); Read the data from input file.

put(char *ch);
Write the data from output file

getline(char *ch); Read the data from input file


File Access Method:-
• There are two ways to access a file:-
1. Sequential Access Method.
2. Random Access Method.
Sequential Access method
• Sequential access to a data file means that the computer system reads or writes
information to the file sequentially, starting from the beginning of the file and
proceeding step by step.

…… 1 2 3 4 5 6
……….
Sequential Access File .
Sequential Access method
• The file Stream classes Supports a number of member functions for
performing input output operations on files.
• The function get() and put() are capable to handle single character at
a time.
• The function getline() let you to handle multiple character at a time.
• The function Read() and Write() are capable to reading and writing
binary data
• The get() and put functions are byte oriented
• That is get() will read the byte of data and put() will write the byte of
data ..
• The get() has many forms but most commonly used version as
shown in below.
• Istream & get (char &ch);
• Ostream&put(char &ch);
• The get function reads a single character associated stream put
that value in ch. It returns reference of stream.
• The put function writes the value of ch to the stream and returns
the reference to the stream.
Random Access File/File
Position Pointer :-
• Random access to file means that the computer system can read or write
information anywhere of the data file.
• This type of operation that also called as direct access because the computer
system knows that where the data is stored using index and goes to “directly” and
reads the data.
Random Access

1 3 2 4 6 5
Random Access File/File Position
Pointer :-
• Both istream and ostream provide member functions for repositioning the
file position pointer.
• This member functions are seekg(“seek get”)for istream and seekp
(“seek put”) for ostream.
• The arguments seekg and seekp are normally long int .
• The get and put arguments can be specified to indicate the seek direction.
• The seek directions are
1. ios::beg-(the default)for positioning the relative to beginning of stream.
2. ios::cur - for positioning the relative to current position in a stream.
3. ios::end - for positioning the relative to end position in a stream.
• The file position pointer is an integer value that specifies the
location in the file as a number of bytes from the files starting
location .
• Example of positioning the “get” the file position pointer are:-
// Position of nth byte of fileobject (ios::beg)
fileobject.seekg(n) ;
// position of n byte forward in fileobject
fileobject.seekg(n,ios::cur) ;
// position of n byte back from end in fileobject
fileobject.seekg(n,ios::end) ;
// position at end of file fileobject
fileobject.seekg(0,ios::end) ;
Difference Between Sequential Access and Random Access File
Sequential Access File Random Access File
Data is entered In sequential order Data is entered with the help of reference
number
Duplicate data is allowed Duplicate data is not allowed

Data is stored in order Data is stored in order based on reference


number

Access is slow Access is fast

Data is stored on tape/ disk Data is stored on disk only

Frequently used Rarely used

You might also like