Professional Documents
Culture Documents
To the
Presentation on
Created by
Bishnu Nayak
In this chapter
Introduction.
2. The fstream.h Header File.
3. Data Files.
4. Opening and Closing File.
5. Steps to Process a File in Our Program.
6. Changing the behavior of Streams.
7. Sequential I/O with Files.
8. Detecting EOF.
9. File pointer and Random Access.
10.Basic Operations on Binary Files.
11.Error Handling During File I/O.
12.Quick revision
1.
1.Introduction
Q: What is a File?
A stream is a sequence of
bytes
Output stream
DISK FILE
M
e
m
o
r
y
File
to
M
e
To
m
o
r
y
File
Bishnu nayak
iostream.h file
Pointer
streamb
uf
ios
istream
ostream
iostream.
h
fstream.h file
ifstrea
m
fstream
fstreamba
se
ofstrea
m
filebuf
3. Data files
Data files
Stored in two ways
For example:
*/
File Modes
It describes how a file is to be used : to write, read etc.
Used as a second argument.
Stream_object.open (filename, (filemode) );
Meaning
Stream type
ios::in
ifstream
ios::out
ofstream
ios::ate
ofstream
ifstream
ios::app
ofstream
ios::trunc
ofstream
exists
ios:nocreate
ofstream
ios::noreplace
ios:binary
ofstream
ofstream
ifstream
If open ( ) is not successful, the stream objects stores Null (Zero) value.
Closing a File
Stream_object close( );
// syntax
ifstream obj;
---------------------
obj.close();
For instance, if a file master is connected with an ofstream object fout, its
connection with the stream fout can be terminated as following:
fout. close( );
Close ( ) flushes the buffer before terminating the connection of the file
with stream.
behaviour
ios:: app
ios:: ate
ios:: trunc
ios::nocreate
ios::noreplace
getline ( ) Functions
Another member function that perform input is getline ( ) whose prototype is
istream & getline (char * buf, int num, char delim = \n);
This func. Is virtually identical to get (buf, num, delim ) version of get( ).
getline() also reads character s from input stream and put them in the array
pointed to by buf until either num characters have been read, or the character
specified by delim is encounter. The default value of delim is newline character.
obj.write((char*)&var, sizeof(var));
obj.read((char*)&var, sizeof(var));
It can detect when end of the file is reached by using the member
function eof ( ) which has the prototype
int eof( );
It returns non zero when the end of the file has been reached, otherwise
it resume to zero.
ifstream fin;
fin.open (Master , ios::in | ios :: binary);
while (! fin.eof ( ))
//as long as eof( ) is zero
{
// that is the files end not reached
:
//process the file
:
} if (fin.eof())
// if nonzero
cout<<End of the file reached ! \n;
It uses eof( ) with the stream object to check for the files end.
Action
fout.seekg(0,ios::beg);
go to start
fout.seekg(0,ios::cur);
fout.seekg(0,ios::end);
go to end of file
fout.seekg(m,ios::beg);
fout.seekg(m,ios::cur);
fout.seekg(-m,ios::cur);
bytes from
go backward by m
the current position
fout.seekg(-m,ios::end);
bytes
go backward by m
from the end
1. Searching
2. Appending Data
Functions
Meanings
int bad ( )
int eof ( )
int fail ( )
int good ( )
Clear ( )
(@_@)
Regarding :
bishnu