-: 0:Source:prog1example.cpp -: 0:Object:prog1example.bb -: 1://********************************************************* v. 9/1/04 -: 2:// Purpose: Compare the values in two files. -: 3:// Can by useful for software testing. -: 4:// Usage: prog1 -: 5:// numberic values are read, one at a time from each file. -: 6:// these two values are compared. -: 7:// Number of values read from each file, along with the -: 8:// number of matches and mismatches are reported. -: 9:// Name: M. Overstreet -: 10://******************************************************************** -: 11:// Listing contents: -: 12:// Purpose -: 13:// Usage -: 14:// Pre & Postconditions -: 15:// Reuse instructions -: 16:// Compilation instructions -: 17:// Known defects -: 18:// Possible enhancements -: 19:// Change history -: 20:// Source code -: 21://******************************************************************** -: 22:// Precondtion: -: 23:// 2 files, each containing numeric values separated by white space. -: 24:// files have only numeric contents. -: 25:// Postcondition: -: 26:// A count of numeric matches and mismatches is produced -: 27://******************************************************************** -: 28:// Reuse instructions: this is so short, it has limited reuse -: 29:// potential, but the code may server as an example of -: 30:// command line parameter passing and error handling. -: 31://******************************************************************** -: 32:// Compilation -: 33:// Under UNIX or Linux: -: 34:// g++ -o prog1 -g prog1.cpp -: 35://******************************************************************** -: 36:// Known defects: -: 37:// 1. doesn't produce required statistics. -: 38:// 2. doesn't handle case where the two files have a different number -: 39:// of values. makes handling eof trickier. -: 40://******************************************************************** -: 41:// Possible enhancements -: 42:// Floats should never by compared for exact matches; the program -: 43:// should determine if numberics values are ints for floats, -: 44:// then compare ints for exact matches, floats for precise -: 45:// matches. -: 46://******************************************************************** -: 47:// Change history: -: 48:// None, it's a new example. -: 49://******************************************************************** -: 50: -: 51:#include -: 52:#include -: 53:#include -: 54:using namespace std; -: 55: -: 56:int main( int argc, char *argv[] ) 1: 57:{ 1: 58: if(argc !=3) -: 59: { #####: 60: cout << "Improper usage: prog1 " << endl; #####: 61: return 1; -: 62: } -: 63: -: 64:// Open and test file stream associated with first file 1: 65: ifstream file1( argv[1] ); 1: 66: if ( !file1 ) -: 67: { #####: 68: cerr << argv[0] << ": " << argv[1] << -: 69: " not a valid file name" << endl; #####: 70: return 1; -: 71: } -: 72: -: 73:// Open and test file stream associated with second file 1: 74: ifstream file2( argv[2] ); 1: 75: if ( !file2 ) -: 76: { #####: 77: cerr << argv[0] << ": " << argv[2] << -: 78: " not a valid file name" << endl; #####: 79: return 1; -: 80: } -: 81: -: 82:// Read data from each file until eof 1: 83: double val1, val2; 1: 84: int matchct = 0, mismatchct = 0; 1: 85: file1 >> val1; 1: 86: file2 >> val2; 4: 87: while ( file1 || file2 ) -: 88: { 3: 89: if ( val1 != val2 ) -: 90: { 1: 91: cout << "mismatch!" << endl; 1: 92: mismatchct++; -: 93: } -: 94: else 2: 95: matchct++; 3: 96: file1 >> val1; 3: 97: file2 >> val2; -: 98: } -: 99: -: 100:// Print run statistics 1: 101: cout << matchct << " matches" << endl; 1: 102: cout << mismatchct << " mismatches" << endl; -: 103: 1: 104:}