Programming and Graphics

(Kiana) #1

142 Introduction to C++ Programming and Graphics


Print a file


In a more advanced application, we generate a binary executable named
pfilethat displays the content of a file with a specified namefilenamein response
to the command:


pfile filenme

This is accomplished by the following code contained in the filepfile.cc:


#include <fstream>
#include <iostream>
using namespace std;

int main ( int argc, char *argv[] )
{
ifstream dev1(argv[1])
if(dev1.isopen() )
{
char x;
while (dev1.get(x))
cout<< x;
}
else
{
cout<<"Unable to open the file" << endl;
}
return 0;
}

The Boolean variabledev1.get(x)is false if the end of the filedev1has been
reached, and true otherwise.


Problems


5.4.1.Add to the programpfile.cca check that issues a warning if no file name,
or more than one file name, is specified.


5.4.2.Write an application that concatenates two files – that is, it creates a
new file consisting of the union of two input files.


5.5 Pointers to functions.........................


Pointers to user-defined functions are employed to concisely represent the func-
tions. Like pointers of regular data types, function pointers can be included in
the arguments of functions to give compound functions.

Free download pdf