





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 9
This page cannot be seen from the preview
Don't miss anything!






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.
▲ 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.
▲ <iomanip.h>: Contains parameterized stream manipulators ▲ <fstream.h>: Contains information important to user-controlled file processing operations
We’ve used streams already like ▲ cin
✓ To or from files ✓ Used similarly as cin, cout
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.
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
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; }
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 } }
✓ 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: