|
int get();
istream& get ( char& c );
istream& get ( char* s, streamsize n );
istream& get ( char* s, streamsize n, char delim );
istream& get ( streambuf& sb);
istream& get ( streambuf& sb, char delim );
Get unformatted data from stream
These member functions perform unformatted input operations. Depending on the type and number of arguments the function behaves in the following way:
- int get();
- Extracts a character from the stream and returns its value (casted to an integer).
- istream& get ( char& c );
- Extracts a character from the stream and stores it in c.
- istream& get (char* s, streamsize n );
- Extracts characters from the stream and stores them as a c-string into the array beginning at s. Characters are extracted until either (n - 1) characters have been extracted or the delimiting character '\n' is found. The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation.
If the delimiting character is found, it is not extracted from the input sequence and remains as the next character to be extracted. Use getline if you want this character to be extracted (and discarded).
The ending null character that signals the end of a c-string is automatically appended at the end of the content stored in s.
- istream& get (char* s, streamsize n, char delim );
- Same as above, except that the delimiting character is the one specified indelim instead of '\n'.
- istream& get (streambuf& sb);
- Extracts characters from the stream and inserts them in the stream buffer sb until either the delimiting character '\n' is found or end of file is reached. The extraction also stops if an error occurs either in the input sequence controled by the stream or in the output sequence controlled by sb.
- istream& get (streambuf& sb, char delim );
- Same as above, except that the delimiting character is the one specified indelim instead of '\n'.
The number of characters read by any of the previous input operations can be obtained by calling to the member function gcount.
Parameters
- c
- A char variable to store the extracted character.
- s
- A pointer to an array of characters where the string is stored as a c-string
- n
- Maximum number of characters to store (including the ternimating null character).
This is an integer value of type streamsize.
- delim
- The delimiting character. The operation of extracting succesive characters is stopped when this character is read. This parameter is optional, if not specified the function considers '\n' (a newline character) to be the delimiting character.
- sb
- An output stream buffer (an object of class streambuf or any of its derived classes).
Return Value
For the first prototype, the function returns the character read. For the remaining prototypes, the function return *this.
Errors are signaled by modifying the internal state flags:
flag | error |
eofbit | The end of the source of characters is reached during its operations. |
failbit | No characters were extracted because either the end was prematurely found or the insertion operation in the destination failed (this only applies to the streambuf case).
Notice that some eofbit cases will also set failbit. |
badbit | An error other than the above happened. |
Additionally, in any of these cases, if the appropriate flag has been set with member function ios::exceptions, an exception of type ios_base::failure is thrown.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
// istream get
#include <iostream>
#include <fstream>
using namespace std;
int main () {
char c, str[256];
ifstream is;
cout << "Enter the name of an existing text file: ";
cin.get (str,256);
is.open (str); // open file
while (is.good()) // loop while extraction from file is possible
{
c = is.get(); // get character from file
if (is.good())
cout << c;
}
is.close(); // close file
return 0;
}
|
This example prompts for the name of an existing text file and prints its content on the screen.
Basic template member declarations
( basic_istream<charT,traits> )
1 2 3 4 5 6 7
|
typedef charT char_type;
int_type get();
basic_istream& get (char_type& c );
basic_istream& get (char_type* s, streamsize n );
basic_istream& get (char_type* s, streamsize n, char_type delim );
basic_istream& get (basic_streambuf<char_type,traits>& sb);
basic_istream& get (basic_streambuf<char_type,traits>& sb, char_type delim );
|
See also
istream::gcount | Get number of characters extracted by last unformatted input operation (public member function) |
|