The C++ cin.get(), cin.eof() and cout.put() methods and end-of-file (EOF) controls code example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Reading from standard input, writing to standard output and controlling the end-of-file C++ programming

To show: How to read from standard input, write to standard output and control the end-of-file in C++ programming

 

// end of file controls depend on the system

// Ctrl-z followed by return key - IBM PC

// Ctrl-d - UNIX and MAC

#include <iostream>

using namespace std;

 

void main(void)

{

char p;

 

cout <<"Using member functions get(), eof() and put()\n"

<<"---------------------------------------------"<<endl;

cout<<"Before any input, cin.eof() is "<<cin.eof()<<endl;

 

// Ctrl+C for PC compatibles and Ctrl+D for UNIX and MAC

cout<<"Enter a line of texts followed by end of file control: "<<endl;

while((p = cin.get()) !=EOF)

cout.put(p);

cout<<"\nAfter some text input, cin.eof() is "<<cin.eof()<<endl;

}

 

Output example:

 

Using member functions get(), eof() and put()

---------------------------------------------

Before any input, cin.eof() is 0

Enter a line of texts followed by end of file control:

This is a test line of text

After some text input, cin.eof() is 1

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index