//********************************************************* v. 9/1/04 // Purpose: Compare the values in two files. // Can by useful for software testing. // Usage: prog1 // numberic values are read, one at a time from each file. // these two values are compared. // Number of values read from each file, along with the // number of matches and mismatches are reported. // Name: M. Overstreet //******************************************************************** // Listing contents: // Purpose // Usage // Pre & Postconditions // Reuse instructions // Compilation instructions // Known defects // Possible enhancements // Change history // Source code //******************************************************************** // Precondtion: // 2 files, each containing numeric values separated by white space. // files have only numeric contents. // Postcondition: // A count of numeric matches and mismatches is produced //******************************************************************** // Reuse instructions: this is so short, it has limited reuse // potential, but the code may server as an example of // command line parameter passing and error handling. //******************************************************************** // Compilation // Under UNIX or Linux: // g++ -o prog1 -g prog1.cpp //******************************************************************** // Known defects: // 1. doesn't produce required statistics. // 2. doesn't handle case where the two files have a different number // of values. makes handling eof trickier. //******************************************************************** // Possible enhancements // Floats should never by compared for exact matches; the program // should determine if numberics values are ints for floats, // then compare ints for exact matches, floats for precise // matches. //******************************************************************** // Change history: // None, it's a new example. //******************************************************************** #include #include #include using namespace std; int main( int argc, char *argv[] ) { if(argc !=3) { cout << "Improper usage: prog1 " << endl; return 1; } // Open and test file stream associated with first file ifstream file1( argv[1] ); if ( !file1 ) { cerr << argv[0] << ": " << argv[1] << " not a valid file name" << endl; return 1; } // Open and test file stream associated with second file ifstream file2( argv[2] ); if ( !file2 ) { cerr << argv[0] << ": " << argv[2] << " not a valid file name" << endl; return 1; } // Read data from each file until eof double val1, val2; int matchct = 0, mismatchct = 0; file1 >> val1; file2 >> val2; while ( file1 || file2 ) { if ( val1 != val2 ) { cout << "mismatch!" << endl; mismatchct++; } else matchct++; file1 >> val1; file2 >> val2; } // Print run statistics cout << matchct << " matches" << endl; cout << mismatchct << " mismatches" << endl; }