Unit 05 File Operation
Unit 05 File Operation
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.
Iostream
Filebuf
Fstream
File
Fstream base
STREAM CLASSES FOR FILE OPEERATION
Stream classes for file operation.
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 :-
put(char *ch);
Write the data from output file
…… 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