0% found this document useful (0 votes)
39 views25 pages

Input and Output Continuation

This document discusses input and output (I/O) with files in C++. It defines a stream as an entity where a program can insert or extract characters sequentially from media like the screen, keyboard, or a file. It describes functions for checking stream state flags like good(), eof(), fail(), and bad(). It also covers getting and putting the stream position using functions like tellg(), tellp(), seekg(), and seekp() to observe and modify the position within a stream where the next read or write operation will occur.
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)
39 views25 pages

Input and Output Continuation

This document discusses input and output (I/O) with files in C++. It defines a stream as an entity where a program can insert or extract characters sequentially from media like the screen, keyboard, or a file. It describes functions for checking stream state flags like good(), eof(), fail(), and bad(). It also covers getting and putting the stream position using functions like tellg(), tellp(), seekg(), and seekp() to observe and modify the position within a stream where the next read or write operation will occur.
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/ 25

I/O WITH FILES

(CONTINUATION)

PEARLY ANN A. ESCALAMBRE


SYLLABUS
OBJECTIVES

Checking Stateflags?
Get and Put Stream Positioning?
WHAT IS STREAM?
C++ uses a convenient abstraction called streams to perform
input and output operations in sequential media such as the
screen, the keyboard or a file. A stream is an entity where a
program can either insert or extract characters to/from.
There is no need to know details about the media associated to
the stream or any of its internal specifications. All we need to
know is that streams are a source/destination of characters, and
that these characters are provided/accepted sequentially.
 CHECKING STATE FLAGS

The following member functions exist to check


for specific states of a stream (all of them return a
bool value):
When an error occurs, FLAGS are set in the state
according to the general category of the error.
GOOD( )

It is the most generic state flag: it returns false in


the same cases in which calling any of the previous
functions would return true. Note that good and
bad are not exact opposites (good checks more
state flags at once).
EOF( )

Returns true if a file open for reading has reached


the end.
FAIL( )

Returns true in the same cases as bad(), but


also in the case that a format error happens,
like when an alphabetical character is
extracted when we are trying to read an
integer number.
BAD( )

Returns true if a reading or writing operation


fails. For example, in the case that we try to
write to a file that is not open for writing or if
the device where we try to write has no space
left.
CLEAR( )

The member function clear() can be used to reset


the state flags.
 GET AND PUT STREAM POSITIONING

All i/o streams objects keep internally -at least- one internal position:
ifstream, like istream, keeps an internal get position with the location of
the element to be read in the next input operation.
ofstream, like ostream, keeps an internal put position with the location
where the next element has to be written.
Finally, fstream, keeps both, the get and the put position, like iostream.
THESE INTERNAL STREAM POSITIONS POINT TO THE LOCATIONS WITHIN THE
STREAM WHERE THE NEXT READING OR WRITING OPERATION IS PERFORMED. THESE
POSITIONS CAN BE OBSERVED AND MODIFIED USING THE FOLLOWING MEMBER
FUNCTIONS:

tellg( ) and tellp( )


These two member functions with no parameters
return a value of the member type streampos,
which is a type representing the current get
position (in the case of tellg) or the put position (in
the case of tellp).
seekg( ) and seekp( )
These functions allow to change the location of the get and put
positions. Both functions are overloaded with two different
prototypes. The first form is:

 seekg ( position );
 seekp ( position );
Using this prototype, the stream pointer is changed
to the absolute position position (counting from the
beginning of the file). The type for this parameter
is streampos, which is the same type as returned by
functions tellg and tellp.
The other form for these functions is:

seekg ( offset, direction );


seekp ( offset, direction );
Using this prototype, the get or put position is set
to an offset value relative to some specific point
determined by the parameter direction.
offset is of type streamoff. and direction is of type
seekdir, which is an enumerated type that
determines the point from where offset is counted
from, and that can take any of the following values:
EXAMPLE:
OUTPUT:
NOTICE THE TYPE WE HAVE USED FOR VARIABLES BEGIN AND
END:
WHAT IS STREAMPOS?

streampos is a specific type used for buffer and file


positioning and is the type returned by file.tellg().
Values of this type can safely be subtracted from
other values of the same type, and can also be
converted to an integer type large enough to
contain the size of the file.
ACTIVITY:

Will be having a 15 items quiz, Review for 5 minutes.


Recorded 1 by 1
GAME RULE

Enter game code


This game will be recorded 1ndividually
Avoid being thrown by a snowball
Snowballs thrown will serve as your score

You might also like