get

C/C++ Reference

get
Syntax:
  #include <fstream>
  int get();
  istream& get( char& ch );
  istream& get( char* buffer, streamsize num );
  istream& get( char* buffer, streamsize num, char delim );
  istream& get( streambuf& buffer );
  istream& get( streambuf& buffer, char delim );

The get() function is used with input streams, and either:

  • reads a character and returns that value,
  • reads a character and stores it as ch,
  • reads characters into buffer until num - 1 characters have been read, or EOF or newline encountered,
  • reads characters into buffer until num - 1 characters have been read, or EOF or the delim character encountered (delim is not read until next time),
  • reads characters into buffer until a newline or EOF is encountered,
  • or reads characters into buffer until a newline, EOF, or delim character is encountered (again, delim isn't read until the next get() ).

For example, the following code displays the contents of a file called temp.txt, character by character:

   char ch;
   ifstream fin( "temp.txt" );
   while( fin.get(ch) )
     cout << ch;
   fin.close();         
Related topics: