Stream Input
Stream-Extraction Operator
Stream input may be performed using the stream-extraction operator:
The overloaded >> operator
The operator is overloaded to provide type-safe input of built-in types (int, char, long, short, unsigned, double, float, etc., plus string types and pointer values)
For example:
//
// main.cpp
// ostream
//
// Created by Bryan Higgs on 9/2/24.
//
#include <iostream>
int main(int argc, const char * argv[])
{
int x, y;
std::cout << "Enter two integers: ";
std::cin >> x >> y;
std::cout << "x = " << x << std::endl
<< "y = " << y << std::endl;
return 0;
}which outputs:
Enter two integers: 56
123
x = 56
y = 123
Program ended with exit code: 0
NOTE: The integers in italic, above, were input to the program.
As with the << operator, you can concatenate operators together.
Looping on Input until End of File
It is common to input a series of items from a stream, until the stream has no more items:
//
// main.cpp
// ostream
//
// Created by Bryan Higgs on 9/2/24.
//
#include <iostream>
int main(int argc, const char * argv[])
{
char name[30];
std::cout << "Name[s]? (end-of-file ends): ";
while (std::cin >> name)
{
std::cout << "Name = '" << name << '\'' << std::endl;
std::cout << "Name? (end-of-file ends): ";
}
std::cout << "Goodbye!" << std::endl;
return 0;
}which outputs:
Name[s]? (end-of-file ends): Bryan
Name = 'Bryan'
Name? (end-of-file ends): Higgs
Name = 'Higgs'
Name? (end-of-file ends): Goodbye!
Program ended with exit code: 0
NOTE: The italicized text was input to the program. The last prompt received a Ctrl/D, which caused an ‘end-of-file’.
There are two code ‘idioms’ for testing for end-of-file:
//
// main.cpp
// ostream
//
// Created by Bryan Higgs on 9/2/24.
//
#include <iostream>
int main(int argc, const char * argv[])
{
char name[30];
std::cout << "Name[s]? (end-of-file ends): ";
while (std::cin >> name)
{
std::cout << "Name = '" << name << '\'' << std::endl;
std::cout << "Name? (end-of-file ends): ";
}
std::cout << "Goodbye!" << std::endl;
return 0;
}//
// main.cpp
// ostream
//
// Created by Bryan Higgs on 9/2/24.
//
#include <iostream>
int main(int argc, const char * argv[])
{
char name[30];
while (true)
{
std::cout << "Name[s]? (end-of-file ends): ";
if ( !(std::cin >> name) )
break;
std::cout << "Name = '" << name << '\'' << std::endl;
std::cout << "Name = '" << name << '\'' << std::endl;
}
std::cout << "Goodbye!" << std::endl;
return 0;
}get and getline
get()
The get member function with no arguments:
int c = cin.get();
inputs a single character from the specified stream.
This version of get returns EOF when end of stream is found.
The difference between >> and get() is that the >> operator normally ignores whitespace characters, while the get() function does not.
Contrast:
while (cin >> c)
cout << c;
with:
while ( (c = cin.get() ) != EOF)
cout.put(c);
get(char &)
The get(char &) member function inputs the next character from the input stream, and stores it in the character argument:
char ch;
// …
cin.get(ch);
It returns:
- a reference to the istream object for which the get is being invoked (this is so it can be chained together), or:
- FALSE when end-of-file is encountered
char ch;
// …
if (cin.get(ch))
cout << ch;
istream &get(char *buffer, int count, char delim = ‘\n’)
There is also a version of get with 3 arguments:
istream &get(char *buffer, int count, char delim = '\n');
which reads characters from the input stream.
It:
- Reads up to one fewer than the count of characters and then terminates, or:
- Terminates when a delimiter character has been read.
It inserts a null termination character in the buffer, at the end of the characters read.
It does not place the delimiter character in the buffer, but does keep it in the stream (I.e. the delimiter character will be the next character read).
char buffer[80];
// …
if ( cin.get(buffer, sizeof(buffer)) )
cout << buffer << endl;
istream &getline(char *buffer, int count, char delim = ‘\n’)
The getline function:
istream &getline(char *buffer, int count, char delim = '\n');
acts just like the get function with the same arguments, except that it removes the delimiter character from the input stream, and discards it:
char buffer[80];
// …
if ( cin.getline(buffer, sizeof(buffer)) )
cout << buffer << endl;
peek, putback, and ignore
ignore skips over a specified number of characters, or terminates when it encounters a specified delimiter:
istream &ignore(int n = 1, int delim = EOF);
putback places the previous character obtained by a get from an input stream, back on to that stream:
istream &putback(char);
peek returns the next character from an input stream without removing it from the stream:
int peek();
All are member functions of the istream class.