#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 8 - Exercise 2 ************* ******** BEGIN EXERCISE REQUIREMENT - THIS MUST BE A "C++" PROGRAM ******** *************************************************************************** (5 points) Write functions OpenFile and DisplayColumns: 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 text 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. DisplayColumns syntax: void DisplayColumns(const char *sFirstColumn, const char *sLastColumn, ifstream &inFile); Parameters: sFirstColumn - a pointer to a string representing a starting column number. sLastColumn - a pointer to a string representing an ending column number. inFile - a reference to an ifstream object representing an open text file Synopsis: Displays the contents of each line in the file in starting with the character in and ending with the character in . If is beyond the last column of a line a blank line is displayed. The display of any line ends when the line itself ends, even if is beyond that point. By definition, the first column of any line is 1, not 0. An empty file is simply closed and ignored. Return: void * Specify the column numbers and file name on the command line using the format: first# last# fileName * Assume no line will contain more than 511 characters. * Assume every character on each line, including the tab, occupies one column. * Do not attempt to store the entire contents of the file. * Test the program three times using file "TestFile1.txt", downloaded from the course web site and the following command line argument lists: 2 23 TestFile1.txt 36 36 TestFile1.txt 61 80 TestFile1.txt Examples: For a file named ABCD containing four lines, labeled below as "1st line", "2nd line", "3rd line", and "4th line": Column# MSD: 0000000001111111111222222222233333333334444444444555... Column# LSD: 1234567890123456789012345678901234567890123456789012... 1st line: This is a typical line in a file. 2nd line: Another one. 3rd line: Short! Long! 4th line: ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 Command line: 13 25 ABCD 1st display: pical line in 2nd display: 3rd display: Long! 4th display: MNOPQRSTUVWXY Command line: 34 60 ABCD 1st display: 2nd display: 3rd display: 4th display: 789 Hint: * Place input test file "TestFile1.txt" 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 int main(int argc, char *argv[]) { const int ARGUMENTS_EXPECTED = 4; // command line args expected if (argc != ARGUMENTS_EXPECTED) // number of cmd. line args { cerr << "Specify: firstColumn lastColumn fileName"; exit(EXIT_FAILURE); } ifstream inFile; OpenFile(argv[3], inFile); // open the file DisplayColumns(argv[1], argv[2], inFile); inFile.close(); // close input file return EXIT_SUCCESS; } #endif