What is iostream in C.

The input and output has basically already been dealt with elsewhere. C ++ uses the concept of the data stream, which is also supported by modern operating systems.

Input via cin

Strings

When reading in character strings, it is not only possible to use objects of type string read in, even C strings can be read into an array. However, caution is required with C strings, as the input does not know the array dimension and therefore does not take it into account. An input that is too long leads to a buffer overflow, i.e. writing to external memory areas.

[Reading in strings]

#include #include using namespace std; int main () {char a [5], b [20]; // or: string a, b; cin >> a >> b; cout << a << "," << b << endl; }

The input object can be queried like a Boolean variable. Then it delivers when EOF is reached. This process is analogous to the check for reaching the end of the file (EOF - End Of File).

[Boolean character]

while (cin >> number) {}

Calling the member function is equivalent to querying the input object directly. The EOF is generated on the keyboard with ctrl-D under UNIX and ctrl-Z under MS-DOS or MS-Windows. Since this type of program control is not easy to convey to a normal user, this method is actually only suitable if files are diverted to the standard input.

Reading in a line

When reading in data via cin the so-called "whitespace", i.e. all spaces, tabs and line feeds, is normally removed as separators. This can be annoying if strings are to be read that can also contain spaces.

getline () global

There is a global function for reading in a complete input line. It expects a stream object as the first parameter. The second parameter is of type string. This has the advantage that no buffer overflow has to be monitored. Optionally, a third parameter can be used to specify what the terminator should be. The end-of-line character is specified. int main () {string FractionString; getline (cin, FractionString) cout << FractionString << endl; }

Member function

The stream classes have a member function. Their first parameter is not a string, but a classic C-String, i.e. a pointer to. Correspondingly, the second parameter is the size of the available buffer. Here, too, it is optionally possible to specify a delimiter. int main () {char fractionString [MAXSTR]; cin.getline (FractionString, sizeof (FractionString)) cout << FractionString << endl; } In all variants, the function always returns a reference to the input stream.

Manipulators

Manipulators are used to prepare and format the output. A few simple manipulators have already been introduced. Manipulators can be inserted between the diversion operators in output streams. In order to use the manipulators, you must first have the file iomanip embed.

End of line

The manipulator generates a line feed and ensures that the line previously output is immediately written to the output unit. This will empty any temporary buffers of the operating system.

Output width setw

As a parameter, the manipulator is given the width that is to be made available at least for the next output object. If the space is larger than required, the output is right-justified. The remaining space is filled with spaces. The space can be changed with the manipulator. It expects a character as a parameter. The manipulator remains in effect until the next time it is called. The flush can be changed with the manipulators or: cout << setw (8) << i << endl; cout << setfill ('-'); cout << setw (8) << i << endl; cout << left << setw (8) << i << endl;

The output of the program looks like this if the variable i has the content 725:

725 -----725 725-----

Number base

A manipulator can be used to set the number base for which all subsequent outputs should appear. There are for decimal numbers, for octal numbers and finally for hexadecimal numbers. Only the pure numbers are displayed. The constant IDs so typical for C and C ++ such as 0x for hexadecimal and a leading 0 for octal are not shown. Such a behavior can be switched on by calling the manipulator. It is switched off again with. cout << oct << endl; cout << setw (8) << i << endl; cout << setfill ('-'); cout << setw (8) << i << endl; cout << hex << left << setw (8) << i << endl;

The output of the program looks like this if the variable i has the content 725:

1325 ---- 1325 2d5 -----

true or 1

Normally 1 is output for the constant and 0 for 0. With some compilers [1] you can change this with the manipulator so that "true" and "false" appear in the output. With 1 and 0 appear again. [Manipulators]
manipulator effect
final Inserts the end of the line
setw (n) Column for next output object is n places wide
left align left-justified
right align right
setfill (c) Use c as a leader
dec Represents numbers in decimal
oct Represents numbers in octal
hex Represents numbers in hexadecimal
showbase Shows 0x in hexadecimal and 0 in octal representation
boolalpha Represents true and false textually
noboolalpha Represents true as 1 and false as 0

Floating point numbers

There are of course also manipulators for floating point numbers. First of all, it also works here to determine the output width. The display type can be switched to. Then the numbers are shown in exponential notation with one digit in front of the decimal point. The manipulator is used to switch to fixed point representation. The manipulator is responsible for the accuracy. The parameter passed to him is how many digits of the number are to be displayed. The manipulators and are not yet implemented in all C ++ compilers. [Floating point manipulators]
manipulator effect
showpoint Representation with decimal point and places after the decimal point
noshowpoint Only the relevant places are output
scientific Floating point number in scientific representation
fixed Fixed decimal places
setprecision (n) Accuracy; n is the number of digits
Instead of placing manipulators in the output stream, member functions of the stream object can also be called. However, the member functions have a slightly different name. The following differences result: cl ({cf. Davis, Stephen R .: C ++ for Dummies, MITP, Bonn, 1999, p. 302.) [Manipulators and element functions]
manipulator Member function
setw (n) width (n)
setfill (c) fill (c)
setprecision (n) precision (n)
The member function is particularly interesting. This function can also be used to functionally simulate the manipulators that are not available in the GNU C ++ compiler 2.95.3, for example. cout.setf (ios :: left); // corresponds to: cout << left;

A manipulator has the advantage over member functions that it can simply be placed in the output stream. A manipulator is recognized by the diversion operator by the fact that it corresponds to one of the following prototypes: (cf. Jakobs, Holger: Introduction to ISO C ++, PDF file of April 12, 2000, p. 75)

[Prototypes for manipulators]

ios & function (ios &); istream & function (istream &); ostream & function (ostream &); When you write a function that has such parameters and return values, you are using it to create a manipulator. The name of the manipulator is the name of the function. So if you want to emulate the manipulator under GNU C ++, create the following function called.

[Self-written left manipulator]

ios & left (ios & i) {i.setf (ios :: left); return i; }

Unfinished

The subject of manipulators is certainly not over yet. You can tell that not all of the manipulators defined in the standard are implemented in the compilers. The function will certainly be used in the programs for some time, for example to output a floating point number with two decimal places:

[Decimal notation]

printf ("% 12.2f \ n", capital);

For the variable capital In the output stream, 12 places are reserved for the sign, the places before and after the decimal point and the decimal point. If fewer digits are required, the value is right-justified. The output always shows two decimal places.


[1]
It works with Borland C ++, but went wrong with GNU C ++.