PP Template

PROGRAMMING POINTERS, LESSON 14

 

 

 

Syntax/correctness issues

 

 

14-1      There are two types of fstream objects you will use to solve file-processing problems: 

 

                  ifstream - for file input from an external source into memory.

                  ofstream - for file output from memory to an external file. 

 

             These file streams are available in fstream.h.

 

 

14-2      There are three file-opening modes:  ios::in, ios::out, ios::app (for append).  However, do not use these modes in a Metrowerks programming environment.

 

 

14-3      There are two options for opening a file.

 

             One line version:

            

                  ifstream  tempFile  ("sample.txt", ios::in);      // Borland Turbo C++ version

                  ifstream  tempFile  ("sample.txt");                // Metrowerks C++ version

 

             Two line version:

 

                  ofstream  tempOutFile;

 

                  tempOutFile.open ("data.txt", ios::out);        //  Borland Turbo C++ version

                  tempOutFile.open ("data.txt");                    //  Metrowerks C++ version

 

 

14-4      Migrating through file directory systems requires different syntax to link internal file names with external permanent files.

 

             In DOS/Windows environments, use a double backslash (\\).  A single backslash moves you into a desired subdirectory.  Because the backslash in C++ serves to introduce special control characters, you will need to use a double backslash to achieve the effect of a single backslash.  Example:

 

             ifstream ("c:\\tc\\sample\\data.txt", ios::in);

 

             In Macintosh environments, use a colon (:) to enter a desired subdirectory.  Example:

 

             ifstream ("Macintosh HD:MW8 Gold:data:sample.txt");

 

 


14-5      When reading integers from a text file, use the idiom below.  The while Boolean expression  (infile >> number)  will continue reading data until the "eof" marker is encountered.  When this occurs, the extraction operator (>>) will not return a file stream, but instead a false value.

            

numshort.txt

 

-3

-2

-1

0

1

2

3

//  Readnum1.cpp  (version 1)

 

#include <iostream.h>

#include <fstream.h>

 

 

main ()

{

       ifstream  infile;

       int  number;

 

       infile.open ("b:\\files\\numshort.txt",ios::in);

       while (infile >> number)

             cout << number << endl;

       return 0;

}

 

Run output:

 

-3

-2

-1

0

1

2

3

                              

                               The loop continues until the stream-extraction operator encounters the eof marker.  At this point the expression evaluates as false and the loop stops.  Notice that all seven values are read from the text file.

 

 

 

Software engineering

 

 

14-6      Try to select identifiers which communication the direction of the flow of data.  For example, inFile is a good choice for data input and outFile is self-documenting for data output.