0% found this document useful (0 votes)
19 views

C++Lec6 Streams and Files

The document discusses C++ streams and files. It covers stream classes, advantages of streams, the stream class hierarchy including ios, istream and ostream classes. It also discusses formatting flags, error status flags, opening, reading and writing to files, file position pointers and provides examples.

Uploaded by

haponabeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

C++Lec6 Streams and Files

The document discusses C++ streams and files. It covers stream classes, advantages of streams, the stream class hierarchy including ios, istream and ostream classes. It also discusses formatting flags, error status flags, opening, reading and writing to files, file position pointers and provides examples.

Uploaded by

haponabeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

C++

Streams and Files


Lecture – 6

Dr. Aghabi Abosaif


October 2023

1
Stream Classes
 A stream is a general name given to a flow of data.

 In C++ a stream is represented by an object of a


particular class.

 The cin and cout stream objects.


Advantages of Streams
 Stream I/O is important if you plan to program in an
environment with a graphical user interface such as
Windows, where direct text output to the screen is not
used.

 So it used to write data to files, and format it in


memory for later use in text input/output windows and
other GUI elements.
Stream class hierarchy
Stream class hierarchy
 You already use of some stream classes:
 The extraction operator >> is a member of the istream class.
 The insertion operator << is a member of the ostream class.

 Both of these classes are derived from the ios class.


 The cout object, representing the standard output
stream, is a predefined object of the ostream_with
assign class, which is derived from the ostream class.

 Similarly, cin is an object of the istream_with assign


class, which is derived from istream.
The ios Class
 The ios class is the base class for the hierarchy.

 It contains many constants and member functions


common to input and output operations of all kinds.

 It contains the majority of the features you need to


operate C++ streams. The three most important features
are
 The Formatting Flags.
 The Error-status Flags
 The File Operation Mode.
The Formatting Flags
 Formatting flags are a set of enum definitions in ios.
 They act as on/off switches that specify choices for various
aspects of input and output format and operation.

 All the flags can be set using the setf() and unsetf() ios
member functions. Look at the following example
ios Formatting Flags
The Error-status Flags
 The stream error-status flags constitute an ios enum
member that reports errors that occurred in an input or
output operation.

 What happens if a user enters the string “nine” instead


of the integer 9?
 Or pushes the Enter key without entering anything?
 Or what happens if there’s a hardware failure?
Error-Status Flags
Error-Status Flags - Example
The File Operation Mode.
 It define different file modes to manipulates with file ,
such as read, write , or read/ write , and other modes will
discussed in next slides.
How to read and write from a file
 To perform file processing in C++, header files <iostream>
and <fstream> must be included in your C++ source file.

 It requires another standard C++ library called fstream,


which defines three new data Types
Data Type Description
ofstream This data type represents the output file stream and is used to
create files and to write information to files.
ifstream This data type represents the input file stream and is used to read
information from files.
fstream This data type represents the file stream generally, and has the
capabilities of both ofstream and ifstream which means it can
create files, write information to files, and read information from
files.
Opening a File
 A file must be opened before you can read from it or
write to it.
 Either ofstream or fstream object may be used to open a
file for writing.
 And ifstream object is used to open a file for reading
purpose only.

 The first argument specifies the name and location of


the file to be opened
 The second argument of the open() member function
defines the mode in which the file should be opened.
The mode – File opened.
The mode – File opened

You can combine two or more of these values by ORing them together.

To open a file in write mode and want to truncate it in case


that already exists,

Open a file for reading and writing purpose as follows


Closing a File
 When a C++ program terminates it automatically flushes
all the streams, release all the allocated memory and
close all the opened files.

 But it is always a good practice that a programmer


should close all the opened files before program
termination??
Writing to a File
 While doing C++ programming, you write information to
a file from your program using the stream insertion
operator (<<) just as you use that operator to output
information to the screen.

 The only difference is that you use an ofstream or


fstream object instead of the cout object.
Reading from a File
 You read information from a file into your program using
the stream extraction operator (>>) just as you use that
operator to input information from the keyboard.

 The only difference is that you use an ifstream or fstream


object instead of the cin object.
Writing Data - Example
Reading Data - Example
Read & Write Example
 Following is the C++ program which opens a file in
reading and writing mode.

 After writing information entered by the user to a file


named afile.dat, the program reads information from the
file and outputs it onto the screen
Read & Write
Example
Read & Write Example

getline() : function to read the line from outside


ignore(): function to ignore the extra characters left by previous read
statement
Read & Write Example -Output
File Position Pointers
 Both istream and ostream provide member functions for
repositioning the file-position pointer.

 The file-position pointer is an integer value that specifies


the location in the file as a number of bytes from the
file's starting location.

 These member functions are


 seekg ("seek get") for istream
 seekp ("seek put") for ostream.
File Position Pointers
 The argument to seekg and seekp normally is a long
integer.
 A second argument can be specified to indicate the seek
direction.
 ios::beg (the default) for positioning relative to the
beginning of a stream.
 ios::cur for positioning relative to the current position in
a stream.
 ios::end for positioning relative to the end of a stream.
" get" file-position pointer -Example
" get" file-position pointer -Example

You might also like