Slide 2.4: Opening and closing files (cont.)
Slide 2.6: Reading and displaying file contents (cont.)
Home

Reading and Displaying File Contents


The program below reads characters from the file test1.txt and write them to the terminal screen.

(un)setting flags                        
#include  <fstream>
#include  <iostream>
  using namespace  std;

int  main( ) {
  char      ch;
  fstream   file;
  file.open ( "test1.txt", fstream::in );
  file. (  );

  while ( 1 ) {
    file >> ch;
    if ( file.eof( ) )  break;
    cout << ch;
  }
  file.close( );
}