cin (input)
and cout (output).
By default, input means
that the user is providing information,
and output means
that the information is printed on the screen.
However, you can also direct files to standard streams.
For example, when running a program like this in Linux,
information is read from input.txt and not from the user:
./code < input.txtSimilarly, you can redirect printing to the
output.txt file as follows:
./code > output.txtIt is also possible to combine these controls as follows:
./code < input.txt > output.txt
int a, b; cin >> a >> b;will read two integers from both of the following feeds:
99 123
99 123If the number of items to read is not known in advance, the following code is handy:
int x;
while (cin >> x) {
cout << "number read " << x << "\n";
}
This code reads integers from the stream
one at a time to x until they no longer exist
(for example, if the stream is a file redirect and the file ends).
The getline function reads and entire line from a stream to a string, including spaces.
For example, the following code reads a line from the stream
into variable s:
string s; getline(cin, s);
#include <iomanip>You can influence the formatting of the output by sending manipulators to the stream before the item to be printed. For example, the manipulator
setw specifies
the width of the field to be printed next:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
cout << setw(4) << i*j;
}
cout << "\n";
}
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25The
setfill manipulator determines
the character to fill in the beginning of the field.
For example, the code below prints the time in the following format
08:30:00:
int h = 8, m = 30, s = 0;
cout << setfill('0');
cout << setw(2) << h << ":";
cout << setw(2) << m << ":";
cout << setw(2) << s << "\n";
Handy manipulators with floating points are
fixed and setprecision.
When the manipulator fixed is first sent to the stream,
then use the setprecision manipulator
can dictate how many decimal places the following
the items are printed.
double x = 3.141592653589793; cout << fixed; cout << setprecision(0) << x << "\n"; // 3 cout << setprecision(2) << x << "\n"; // 3.14 cout << setprecision(5) << x << "\n"; // 3.14159Note that
setw only works on the next
printable item, but the other manipulators described above
affect all printing thereafter.
#include <fstream>The file stream
ifstream is for reading data
from a given file.
For example, the following code reads two integers
from input.txt:
ifstream f("input.txt");
int a, b;
f >> a >> b;
f.close();
Similarly, the file stream ofstream
prints information to a given file.
For example, the following code prints the "Hello!"
to the file output.txt.
ofstream f("output.txt");
f << "Hello!\n";
f.close();
By default, the file will be cleared before output.
However, the ios::app (append)
lets you dictate that the new information is ouput to the end of the file:
ofstream f("output.txt", ios::app);
#include <sstream>A string stream allows you to read information from a string into variables. For example, the following code creates a string stream and uses it to read two integers.
string s = " 99 123 "; stringstream ss(s); int a, b; ss >> a >> b; cout << a << " " << b << "\n"; // 99 123The following code uses a string stream to form a string, with a floating point number displayed to two decimal places.
stringstream ss; double x = 3.141592653589793; ss << fixed << setprecision(2) << x; string s = ss.str(); cout << s << "\n"; // 3.14