Reads and displays a file. - C++ File Stream

C++ examples for File Stream:File Operation

Description

Reads and displays a file.

Demo Code

#include <fstream>
#include <iostream>
using namespace std;
#include <stdlib.h>
ifstream fp;//w  ww. ja  v  a2s  .  c om
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;
}

Result


Related Tutorials