Unit#2 Control Structures - Selection (If statements)
Return to Unit
Data Files and Report Files

There are many instances when it is inconvenient to enter all data for our program using the keyboard. It may be OK to enter a few of the required numbers, but we may want to have most of our input typed earlier in the form of a file and then have the program read the numbers directly from the file.  Similarly in many cases we prefer to have most of the output of the program go to a file ( which can be saved, printed , attached to an e-mail etc. ) rather than have all the results displayed on the default black window on our screen.  Again, it may be appropriate to print something to the screen, say " your program ran successfully.  Your results are stored in file results.txt" .

Let's look at the following diagram:

 

Now let's see how you name the streams. You must use commands, which look like this:

ifstream dataA ("adata.txt", ios:: in);

 ifstream dataB ("bdata.txt", ios:: in);

 ofstream mytable ("Report.txt", ios:: out);

 here ifstream stands for input file stream and ofstrem stands for output file streamin order to use these statements, we must include a special header: #include<fstream.h>

An example is given here.  Click on the files to see their contents:

DATA FILE :  2ex1dat.txt 
PROGRAM : 2ex1.appended


 

Jacob Y. Kazakia © 2001 All rights reserved