C++ Input/Output Streams: Understanding Sequential Data Flow, Lecture notes of Computer Programming

An in-depth exploration of c++ streams, a popular concept for handling input/output operations. Streams are sequences of characters with functions to extract or insert data. Console streams connected to the keyboard and screen, as well as file streams connected to the file system. It explains the iostream library, predefined streams, stream state, and formatting manipulators. Students will gain a solid understanding of how to use streams for efficient data handling in c++.

Typology: Lecture notes

2017/2018

Uploaded on 11/09/2018

hailetesfay418
hailetesfay418 🇪🇹

1 document

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 6
C++ Input/output Streams
A stream is a popular concept for how to do input/output. Basically, a stream is a sequence of
characters with functions to take characters out of one end, and put characters into the other end.
In the case of input/output streams, one end of the stream is connected to a physical I/O device
such as a keyboard or display. If it is a console output stream, your program puts characters into
one end of the stream, and the display system takes characters out of the other and puts them on
the screen. If it is a console input stream, the keyboard puts characters into one end of the stream,
and your program takes characters out of the other and stores the results in variables in the
program. If no characters are waiting in the input stream, your program must wait until you
supply some by typing on the keyboard.
File streams follow the same principle, except that the file system is attached to the other end of
the stream.
1. Streams and Files
A stream is a general name given to a flow of data.
In C++, a stream is represented by an object of a particular stream class.
Different streams are used to represent different kinds of data flow. In particular, streams are
used for file I/O.
Input stream
Flow into program
Can come from keyboard
Can come from file
Output stream
Flow out of program
Can go to screen
Can go to file
2. iostream library Header files
<iostream.h>: Contains cin, cout, cerr, and clog objects
FUNDAMENTAL OF PROGRAMMING IIPage 1
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download C++ Input/Output Streams: Understanding Sequential Data Flow and more Lecture notes Computer Programming in PDF only on Docsity!

Chapter 6

C++ Input/output Streams

A stream is a popular concept for how to do input/output. Basically, a stream is a sequence of characters with functions to take characters out of one end, and put characters into the other end. In the case of input/output streams, one end of the stream is connected to a physical I/O device such as a keyboard or display. If it is a console output stream, your program puts characters into one end of the stream, and the display system takes characters out of the other and puts them on the screen. If it is a console input stream, the keyboard puts characters into one end of the stream, and your program takes characters out of the other and stores the results in variables in the program. If no characters are waiting in the input stream, your program must wait until you supply some by typing on the keyboard. File streams follow the same principle, except that the file system is attached to the other end of the stream.

  1. Streams and Files

▲ A stream is a general name given to a flow of data. ▲ In C++, a stream is represented by an object of a particular stream class. ▲ Different streams are used to represent different kinds of data flow. In particular, streams are used for file I/O.

  • Input stream ✓ Flow into program ▲ Can come from keyboard ▲ Can come from file
  • Output stream ✓ Flow out of program ▲ Can go to screen ▲ Can go to file
  1. iostream library Header files ▲ <iostream.h>: Contains cin, cout, cerr, and clog objects

▲ <iomanip.h>: Contains parameterized stream manipulators<fstream.h>: Contains information important to user-controlled file processing operations

  1. C++'s Predefined Streams When a C++ program begins execution, four built-in streams are automatically opened. They are:

We’ve used streams already like ▲ cin

  • Input stream object connected to keyboard ▲ cout
  • Output stream object connected to screen Can define other streams

✓ To or from files ✓ Used similarly as cin, cout

  1. Stream state

4.1. Formatted I/O Format state flags specify formatting to be performed during stream I/O operations like setf , unsetf and flags which are Member functions that control the flag settings Format State Flags

▲ Defined as an enumeration in class ios ▲ Can be controlled by member functions ▲ flags - specifies a value representing the settings of all the flags ✓ Returns long value containing prior options

Table 6.1 the iostream library From the base class ios, we have a derived class ( istream , ostream) So, iostream support both stream input and output. The class hierarchy is shown below.

ios is

the

bas

Stream Manipulators

A manipulator is an identifier that can be inserted into an output stream or extracted from an input stream in order to produce a desired effect. For example, endl is a commonly - used manipulator which inserts a newline into an output stream and flushes it. Therefore, cout << 10 << endl; has the same effect as: cout << 10 << ' \n'; In general, most formatting operations are more easily expressed using manipulators than using setf. For example, cout << oct << 10 << endl; is an easier way of saying: cout.setf(ios::oct, ios::basefield); cout << 10 << endl; Some manipulators also take parameters. For e xample, the setw manipulator is used to set the field width of the next IO object: cout << setw(8) << 10; // sets the width of 10 to 8 characters The following Table summarizes the predefined manipulators of the iostream library and the description is illustrated below the table with example.

Table 6.2. Stream manipulator

  1. Stream Base

For stream base we have:

Program example: //using hex, oct, dec and setbase stream manipulator #include <stdlib.h> #include <iostream.h> #include <iomanip.h>

cout<<setprecision(poinplace)<<theroot<<endl; }

  1. Field Width

Sets the field width and returns the previous width. If values processed are smaller than the field width, fill characters are inserted as padding. Wider values will not betruncated. ✓ Use width()or setw(). For example: cout.width(6); //field is 6 position wide ✓ Programexample: //using width member function #include <iostream.h> #include <stdlib.h> void main(void) {int p = 6; char string[20]; cout<<"Using field width with setw() or width()"<<endl; cout<<"----------------------------------------"<<endl; cout<<"Enter a line of text:"<<endl; cin.width(7); while (cin>>string) {cout.width(p++); cout<<string<<endl; cin.width(7); //use ctrl-z followed by return key or ctrl-d to exit } }

  1. Stream Format States

✓ Format state flag specify the kinds of formatting needed during the stream operations. ✓ Available member functions used to control the flagsetting are: setf(), unsetf()and flags ().

✓ flags()function must specify a value representing the settings of all the flags.

✓ The one argument, setf()function specifies one or more ORed flags and ORs them with the existing flag setting to form a new format state. ✓ The setiosflags()parameterized stream manipulator performs the same functions as the setf. ✓ The resetiosflags()stream manipulator performs the same functions as the unsetf ()member function. For parameterized stream manipulators you need iomanip.h header file. ✓ Format state flags are defined as an enumeration in class ios. The list for some of the flags is shown below: