Overloaded functions to retrieve a char or a char sequence from an input stream.
int_type get();
Extracts a character if available and returns that value, otherwise calls setstate(failbit) and returns eof().
basic_istream<charT, traits>& get(char_type& c);
Extracts a character and assigns it to c if possible, else calls setstate(failbit).
basic_istream<charT, traits>& get(char_type* s, streamsize n, char_type delim = traits::newline());
Extracts characters and stores them in a char array at an address pointed to by s, until:
If end_of_file is encountered, setstate(eofbit) is called.
If no characters are extracted setstate(failbit)is called. In any case, it stores a null character in the next available location of array s.
basic_istream<charT, traits>& get (basic_steambuf<char_type, traits>& sb, char_type delim = traits::newline());
Extracts characters and assigns them to the basic_streambuf object sb if possible or else it calls setstate(failbit). Extraction stops if:
Returns an integer when used with no argument. When used with an argument, if a character is extracted, the get() function returns the this pointer. If no character is extracted setstate(failbit) is called. In any case a null char is appended to the array.
// READ ONE CHARACTER: // ewl-test file for input // float 33.33 double 3.16e+10 Integer 789 character C #include <iostream> #include <fstream> #include <cstdlib> int main() { using namespace std; char inFile[] = "ewl-test"; ifstream in(inFile); if(!in.is_open()) {cout << "Cannot open input file"; exit(1);} char ch; while(in.get(ch)) cout << ch; return 0; }
//float 33.33 double 3.16e+10 Integer 789 character C
// READ ONE LINE:
#include <iostream>
const int size = 100;
char buf[size];
int main()
{
using namespace std;
cout << " Enter your name: ";
cin.get(buf, size);
cout << buf;
return 0;
}
Result:
Enter your name: Johnny Socksorter<enter> Johnny Socksorter