C++ examples for File Stream:File Operation
Reads and displays a file.
#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; }