#define ALLOW_INSTRUCTOR_CODE /*************************************************************************** *************************************************************************** ****** BEFORE BEGINNING OR TURNING IN THIS EXERCISE IT IS EXTREMELY ****** ****** IMPORTANT THAT YOU READ, FULLY UNDERSTAND, AND ADHERE TO THE ****** ****** REQUIREMENTS STATED IN THE DOCUMENT TITLED, "ASSIGNMENT ****** ****** SUBMISSION REQUIREMENTS", DOWNLOADABLE FROM THE COURSE WEB ****** ****** SITE. ****** *************************************************************************** **************************************************************************/ /*************************************************************************** ************* C/C++ Programming 2: Assignment 7 - Exercise 2 ************* ******** BEGIN EXERCISE REQUIREMENT - THIS MUST BE A "C++" PROGRAM ******** *************************************************************************** (2 points) Write functions OpenFile and ListHex: OpenFile syntax: void OpenFile(const char *fileName, ifstream &inFile); Parameters: fileName - a pointer to the name of the file to be opened inFile - a reference to the ifstream object to be used to open the file Synopsis: Opens the file named in in the read-only binary mode using the object. If the open fails an error message is output to cerr and the program is terminated with an error exit code. Return: void if the open succeeds; otherwise, the function does not return. ListHex syntax: void ListHex(ifstream &inFile); Parameters: inFile - a reference to the ifstream object for a file that is open in a readable binary mode Synopsis: Displays the contents of the file in as two-character hexadecimal bytes, zero-filled on the left if necessary to produce two characters. 16ea space-separated pairs are placed on each line and pairs are aligned from one line to the next. The last line will contain less than 16 pairs if EOF is reached prior to completion of that line. Return: void Typical output from ListHex should look like: 00 AB 05 72 FE 01 03 67 68 69 20 40 78 0A 0D 02 AB 05 72 FE 01 AB 05 72 FE 01 20 40 78 67 68 69 FE 01 03 67 68 69 20 * OpenFile must open the file in the binary mode regardless of how it was created or what it contains. * Do not print any bytes that are not actually in the file * Do not print EOF; it is not a character in the file * Write ListHex so that the number of pairs printed per line can be changed, that is, don't embed the 16 in the printing code. * Test the program on files "TestFile3.txt" and "TestFile4.bin", downloaded from the course web site. Hint: * Place input test files "TestFile3.txt" and "TestFile4.bin" in the appropriate default location described in the "FILE LOCATION QUESTIONS AND ANSWERS" section of the "ASSIGNMENT SUBMISSION REQUIREMENTS" document. *************************************************************************** ************************ END EXERCISE REQUIREMENT ************************* **************************************************************************/ /*************************************************************************** * BEFORE PRINTING THIS ASSIGNMENT TO TURN IN, DELETE THIS COMMENT AND * EVERYTHING ABOVE IT. **************************************************************************/ /* * TODO: * REPLACE THIS COMMENT WITH YOUR TITLE BLOCK. */ /* * TODO: * REPLACE THIS COMMENT WITH YOUR C++ CODE, PLUS ANYTHING NECESSARY TO * SUPPORT MY TEST CODE BELOW. DO NOT INCLUDE ANY UNNEEDED HEADER FILES! */ /*************************************************************************** * BEFORE PRINTING THIS ASSIGNMENT TO TURN IN, DELETE THIS COMMENT AND * EVERYTHING BELOW IT. **************************************************************************/ /* ***** Change nothing below this comment without instructor permission. **** * Everything that follows was written to help test/verify your code. You * do not need to understand the details of my code to write yours! */ #ifdef ALLOW_INSTRUCTOR_CODE #define Elements(array) (sizeof(array)/sizeof((array)[0])) const char * const FILENAMES[] = {"TestFile3.txt", "TestFile4.bin"}; int main() { for (int i = 0; i < int(Elements(FILENAMES)); ++i) { ifstream inFile; OpenFile(FILENAMES[i], inFile); cout << "Hex dump of file " << FILENAMES[i] << ":\n"; ListHex(inFile); inFile.close(); cout << '\n'; } return EXIT_SUCCESS; } #endif