C++ input/output (I/O)

Streams are used for input and output. When you type at the keyboard and your program takes your input as data, that is standard input. When your output is displayed at the terminal screen, that is standard output. The standard input stream is called cin, pronounced like two words: see in . The standard output stream is called cout, also pronounced like two words: see out .

To get basic I/O functionality, you must #include < iostream > . To get basic formatting functionality, #include < iomanip > . To get basic I/O functionality reading or printing to a file, #include < fstream > .

The extraction operator, >>
The extraction operator, >>, is for input. Think "extract from the input stream" for this operator. It is also commonly referred to as the input operator.

The extraction/input operator essentially grabs one value from the input stream each time it is called. Consider the code to read ints from standard input:
   int i, j, k;
   cin >> i >> j >> k;
If the user types in three integers, say 5, 6, 7, they will be assigned to i, j, k respectively. These can be typed on the same line, separated by blanks or tabs, or on different lines. The >> operator skips over all white space. It is equivalent to write three cin lines:
   int i, j, k;
   cin >> i;
   cin >> j;
   cin >> k;
The insertion operator, <<
The insertion operator, << is for output. Think "insert into the output stream" for this operator. It is also commonly referred to as the output operator.

The insertion/output operator inserts one value into the output stream each time it is called. Consider the code to display to standard output:
   int i = 10, j = 20, k = 30;
   cout << i << j << k;
The output from this looks like:
102030
Since this is not usually how you want your output to look, use strings of blanks to separate values, or use setw, read "set width", to set the width of the printing field of numbers. Blanks are padded on the left of the number (right justified). You must #include < iomanip > to get setw. Use endl, read "end line", to display a line break (put output on the next line).
   cout << "The values are: " << i << "  " << j << "  " << k << endl;
   cout << setw(5) << i << setw(5) << j << setw(5) << k;
The output from this code looks like
The values are: 10  20  30
   10   20   30
Typical formatting of long output lines emphasizes the cout:
   cout << "The values are " << i << " and " << j << " and " << k 
        << " followed by " << i+j+k << endl;
Reading from a file
When you read from a file, you must #include < fstream > . All the reading works in the same way as described above, but you use the filename instead of cin.

Create the file object and open the datafile. When the file object goes out of scope it is closed. Always check to see if the file was opened (check to see that the object file is defined). The cerr is where errors are written. Then read using the >> operator.
   // create file object and open the datafile 
   ifstream infile("someFilename.txt");
   if (!infile) {
      cerr << "File could not be opened." << endl;
      return false;
   }

   int i, j, k;
   infile >> i >> j >> k;
Output is similar. Create an object of type ofstream and use the << operator in the same way as described above.

When passing the file object in as a parameter, always use pass by reference since the file object, the address of the next thing to read, is continually changing and that updated address needs to be used for the next reading. For example
   void SomeClassName::buildSomething(ifstream& infile) {
      ...
   }