One of the challenges of porting code from one platform to another is that programming languages aren't always standardized, i.e., they may implement features differently, with the result that the same code may not execute the same way on the different platforms. This means modifications in the code may be necessary.
An example of this is the C++ eof() function, which should return true or false according to whether we have reached the end of file. That is, we are reading data from a text (ASCII) file, and the eof() function should return
Unfortunately, compilers available to CIS students implement this function differently:
A simple illustration of the difference this makes is shown below. Suppose we have an input file with a list of numbers, one numeric entry per line, that we wish to total. Compare how a function to total the file would have to be written for these two implementations of the eof() function. You might do this by tracing the execution of each version through its processing of a small file (say, with two lines of data).
Borland, Gnu C++ versionvoid totalFile(char * fName, float & total)
{ fstream rawData;
float itemPrice;
total = (float) 0.0;
rawData.open(fName, ios::in);
while(!rawData.eof()) { // (g0)
rawData >> itemPrice; // (g1)
/* Since the input above might cause us to read past
the end-of-file, we must check to ensure we don't
add the last input value a second time. */
if(!rawData.eof()) // (g2)
total += itemPrice; // (g3)
} // end while
rawData.close();
} // end echoAndTotal
|
MS Visual C++ versionvoid totalFile(char * fName, float & total)
{ fstream rawData;
float itemPrice;
total = (float) 0.0;
rawData.open(fName, ios::in);
while(!rawData.eof()) { // (m0)
rawData >> itemPrice; // (m1)
total += itemPrice; // (m2)
} // end while
rawData.close();
} // end echoAndTotal
|
|
Consider how these execute on a file of two entries, say 5.00 Both versions initialize total as 0, open the input file, and begin their loops. However, their loops execute somewhat differently in order to produce the same final value of total, as discussed below: |
|
|
|