Remarks
Extracts characters and stores them in a
char array at an address pointed to by
s, until:
- a limit (the second argument minus one) or the number of characters to be stored is reached
- a delimiter (the default value is the newline character) is met. In which case, the delimiter is not extracted.
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:
- an insertion fails
- end-of-file is encountered
- an exception is thrown
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.
basic_istream::getline
// 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