Character Input/Output Using get/put

Currently, we output characters using the insertion operator:
   char someChar;         // declare a char
   ofstream outFile;      // declare an output stream

   . . .

   cout << someChar;      // write char to display
   outFile << someChar;   // write char to output file
Alternatively, we can use put:
   cout.put(someChar);     // write char to display
   outFile.put(someChar);  // write char to output file
We input characters using the extraction operator:
   char someChar;         // declare a char
   ifstream inFile;       // declare an input stream

   . . .

   cin >> someChar;       // read char from keyboard
   inFile >> someChar;    // read char from input file
Alternatively, we can use get for input:
   cin.get(someChar);     // read char from keyboard
   inFile.get(someChar);  // read char from input file
We've seen cin.get() already and know that it is used to read white space that the extraction operator skips.

Previous page
Next page

Back to Lesson 13 Index
Back to Outline