SOURCE FILE: myCat.cpp
/* The purpose of this program is to get the
name of a text file from the user and
print the file to standard output.
It was written to illustrate how to use
strings, files and functions with a C++
program. */
/* 'include statements' and 'using'
directives that allow the program to use
functions and variables that are in
libraries. */
#include <fstream>
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
/* function prototypes --
to let the compiler know
the functions exist. */
string GetFileName () ;
void PrintFile (string filename) ;
/* The main function - every C++ program
has exactly one main function. This is
where execution always begins. */
int main()
{ /* declare a variable of type 'string' */
string filename ;
/* Call a function and capture the string returned */
filename = GetFileName () ;
/* Call a function to print the file */
PrintFile (filename) ;
return 0 ;
}
/* Definition of the GetFileName function
The function heading indicates that this
function returns a string and has no
parameters. */
string GetFileName ()
{
string file_name ;
/* Output a prompt to the standard output stream */
cout << "Please type the name of the file you would like to see: " ;
/* Input the string the user enters from standard input */
cin >> file_name ;
/* Return file_name to the caller - and
halt execution of this function */
return file_name ;
}
/* Definition of the PrintFile function
The 'void' keyword starting the function
heading indicates that this function does
not return a value. The parameter list
shows that it has a value-parameter of
type string. */
void PrintFile (string filename)
{
/* Output a blank line to standard output */
cout << endl ;
/* Declare a file variable 'inFile', bind
it the file, and open the file. */
ifstream inFile (filename.c_str()) ;
/* Declare a variable of type char (character) */
char nextChar ;
/* We continue a loop while the
operation of getting the next
character from the file is
successful. */
while (inFile.get(nextChar) )
{
/* We simply write each character we read.
We write to standard output. */
cout << nextChar ;
}
cout << endl ;
/* Close the file */
inFile.close() ;
}