C++ ifstream Reads and displays a file

Description

C++ ifstream Reads and displays a file

#include <fstream.h>
#include <stdlib.h>
ifstream fp;/*w  w  w. j a  v  a 2s  . com*/
void main()
{
   char filename[12]; // Holds user's filename.
   char in_char;      // Input character.
   cout << "What is the name of the file you want to see? ";
   cin >> filename;
   fp.open(filename, ios::in);
   if (!fp)
   {
      cout << "\n\n*** That file does not exist ***\n";
      exit(0);    // Exit program.
   }
   while (fp.get(in_char))
   {
      cout << in_char;
   }
   fp.close();
   return;
}



PreviousNext

Related