0% found this document useful (0 votes)
18 views16 pages

Oops Unit3

The document discusses methods for detecting the end-of-file condition in file handling, including using a while loop and the eof() function. It also outlines various file modes available in the ios class, detailing their effects on file operations, and explains file pointers and their manipulation using functions like seekg() and tellg(). Additionally, it describes how to specify offsets for file pointers and the actions associated with different seek calls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views16 pages

Oops Unit3

The document discusses methods for detecting the end-of-file condition in file handling, including using a while loop and the eof() function. It also outlines various file modes available in the ios class, detailing their effects on file operations, and explains file pointers and their manipulation using functions like seekg() and tellg(). Additionally, it describes how to specify offsets for file pointers and the actions associated with different seek calls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

DETECTING END-OF FILE:

• Detection of the end-of-file condition is necessary for


preventing any further attempt to read data from the
file.
while(fin)
• Here, the while loop terminates when fin returns a
value of zero on reaching the end–of–file condition.
• This loop may terminate due to other failures as well.
• we have another one approach to detect end-of-file.
if(fin.eof()!=0)
{
exit();
}

• eof is a member function of ios class.


• The eof returns a non zero value if the end-of-file (EOF)
condition is encountered, otherwise zero.
• The above statement terminates the program on
reaching the end of the file.
• FILE MODES:

• We have used IFSTREAM and OFSTREAM constructors


and the function OPEN() to create a file as well as to
open the existing file.
• The OPEN() function can take two arguments, the
second one for specifying the file mode.
• SYNTAX:
stream.object.open(“filename”,mode);

• The second argument specifies the purpose for


which the file is opened.
• The file mode parameter can take one or
more constants in ios class.

MODE VALUE EFFECT ON THE MODE


Ios::in Open for reading
Ios::out Open for writing
Ios::ate Go to the end of the file at opening
time.

Ios::app Append mode:all writes occur at


end of file.

Ios::trunc Truncate the file if it already


exists.
Ios::nocreate Open fails if file
does not exist.
Ios::noreplace Open fails if file
already exists.
Ios::binary Open as a binary
file.
• Opening a file in ios::out mode also opens it
in the ios::trunc mode by default.
• Both ios::app and ios::ate take us to the end-
of-file when it is opened.
• The difference between them is
• ios::app allows us to add data to the end-of-
file only.
• ios::ate mode permits us to add data or
modify the existing data anywhere in the file.
• The mode combine two or more parameters
fout.open(“data”,ios::app | ios::nocreate)
FILE POINTERS AND THEIR MANIPULATIONS:

• Each file has two pointers which are knows as file


pointer. They are

1) input pointer (get pointer)


2) output pointer (put pointer)

• We can use this pointer to move through the files while


reading and writing.
• An input or output operation takes place, the
appropriate pointer is automatically advanced.
DEFAULT ACTIONS:

CASE (1)
• when we open a file in read-only mode, the input
pointer is automatically set at the beginning of the file,
so that we can read from the start

H E L L O WO R L D

input pointer

CASE(2):
• when we open a file in write-only mode, the existing
contents are deleted and the output pointer is set at
the beginning.

output pointer

CASE(3):
• when we open a file in append mode, the output pointer is set at the
end of file. (ie., end of the existing contents)

H E L L O W O R L D


output pointer
FUNCTIONS FOR MANIPULATION OF FILE POINTERS:
• How do we move a file pointer to a desired position inside the file?
• for that, we are using in-build functions which are supported by file
stream classes. 
FUNCTIONS ACTION PERFORMED

Seekg() Moves get file pointer to a


specific location
Seekp() Moves put file pointer to a
specific location
Tellg() Returns the current
position of the get pointer

Tellp() Returns the current


position of the put pointer.
• For example,
infile.seekg(10);

• Moves the input file pointer to the byte number 10.


• The bytes in a file are numbered beginning from
zero . Therefore , the pointer will pointing to the 11th
byte in the file.
• Consider the following example.

ofstream fileout;
fileout.open(“hello”,ios::app);
int p=fileout.tellp();
• The output pointer is moved to the end of the file
“hello” and the value of p will represent the number of
bytes in the file.

SPECIFYING THE OFFSET:

• seek function is used to move the file pointer to a


desired location.
• The argument to these functions represents the
absolute position in the file.
start
FILE
end



  M bytes

Outfile.seekp(m);
seek functions seekg() and seekp() can also used
with two arguments as follows.

seekg(offset,repostion);
Offset- represents the of bytes the
• Offset – represents the number of bytes the file
pointer is to be moved from the location
specified by the reposition
• Reposition – reposition takes one of the
following three constants specified in the ios
class

ORIGIN VALUE SEEKS FROM….

Ios::beg Start of the file


Ios::cur Current position of pointer
Ios::end End of file
SEEK CALL ACTION
Fout.seekg(0,ios::beg) Go to the start

Fout.seekg(0,ios::cur) Stay at the current


positon
Fout.seekg(0,ios::end) Go to the end of file

Fout.seekg(m,ios::beg) Move to(m+1) byte in


the file
Fout.seekg(m,ios::cur) Go forward by m bytes
from the current
position
Fout.seekg(-m,ios::cur) Go backward by m
bytes from the current
position
Fout.seekg(-m,ios::end) Go backward by m
bytes from the end

You might also like