Page 280 - DCAP404 _Object Oriented Programming
P. 280
Unit 13: Working with Files
Keyboard
Notes
Figure 13.1 (c): Output Stream Currently Attached to Monitor
Monitor
Figure 13.1 (d): Output Stream Currently Attached to a Data File
Fig
File
Figure 13.1 (e): Output Stream Currently Attached to a Printer
Figure
C++ library provides prefabricated classes for data streaming activities. In C++, the file stream
classes are designed with the idea that a file should simply be viewed as a stream or array or
sequence of bytes. Often the array representing a data file is indexed from 0 to len-1, where len
is the total number of bytes in the entire file.
A file normally remains in a closed state on a secondary storage device until explicitly opened
Figure
by a program. A file in open state file has two pointers associated with it:
1. Read pointer: This pointer always points to the next character in the file which will be read
if a read command is issued next. After execution of each read command the read pointer
moves to point to the next character in the file being read.
2. Write pointer: The write pointer indicates the position in the opened file where next
character being written will go. After execution of each write command the write pointer
moves to the next location in the file being written.
These two file positions are independent, and either one can point anywhere at all in the file. To
elucidate I/O streaming in C++ let us write a small program.
#include <fstream.h>
int main()
{
ofstream filename(“c:\cppio.dat”);
LOVELY PROFESSIONAL UNIVERSITY 273