Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

INSTRUCTOR SUPPLIED SOURCE CODE: #define INSTRUCTOR_FILE #ifdef INSTRUCTOR_FILE #include #include using std::ifstream; ifstream *OpenFiles(char * const fileNames[], size_t count); void MergeAndDisplay(ifstream files[], size_t count); int

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

INSTRUCTOR SUPPLIED SOURCE CODE:

#define INSTRUCTOR_FILE #ifdef INSTRUCTOR_FILE

#include #include using std::ifstream;

ifstream *OpenFiles(char * const fileNames[], size_t count); void MergeAndDisplay(ifstream files[], size_t count);

int main(int argc, char *argv[]) { const int FILE_NAME1_ARG_NO = 1; // command line arg: first file name

ifstream *files = OpenFiles(&argv[FILE_NAME1_ARG_NO], size_t(argc) - 1); MergeAndDisplay(files, size_t(argc) - 1); delete[] files;

return EXIT_SUCCESS; } #endif

M-FILES FOR TEST:

mFile1.txt:

1 / 1 1 / 2 1 / 3 1 / 4 1 / 5 1 / 6 1 / 7 1 / 8 1 / 9 1 / 10 1 / 11 1 / 12 1 / 13 1 / 14 1 / 15 1 / 16 1 / 17 1 / 18

mFile2.txt:

2 / 1 2 / 2 2 / 3 2 / 4 2 / 5 2 / 6 2 / 7 2 / 8 2 / 9

mFile3.txt: Was Empty

mFile4.txt:

4 / 1 4 / 2 4 / 3 4 / 4 4 / 5 4 / 6 4 / 7 4 / 8 4 / 9 4 / 10 4 / 11 4 / 12 4 / 13

mFile5.txt:

5 / 1 5 / 2 5 / 3

C2A8E1 (10 points - C++ Program) Exclude any existing source code files that may already be in your IDE project and add a two new ones, naming them C2A8E1_OpenFiles.cpp and C2A8E1_Merge AndDisplay.cpp. Also add instructor-supplied source code file C2A8E1_main-Driver.cpp. Do not write a main function! main already exists in the instructor-supplied file and it will use the code you write. File C2A8E1_OpenFiles.cpp must contain a function named OpenFiles. OpenFiles syntax ifstream *OpenFiles (char * const fileNames[], size_t count); Parameters: fileNames - a pointer to the first element in an array representing the names of text files to be opened. The array has the following standard ragged array format: char *fileNames[] = { "file", "file", etc. }; count - the number of elements in fileNames Synopsis: Dynamically creates an array of ifstream objects having count elements then uses those objects to open the files named in fileNames, in order. All opens are in the read-only text mode. If any open fails all previously opened files are explicitly closed, the dynamic allocation is deleted, an error message is output to cerr, and the program is terminated with an error exit code. The error message must mention the name of the failing file. If count is zero an error message to that effect is output to cerr and the program is terminated with an error exit code. Return: a pointer to the first entry in the ifstream array if count is non-zero and all opens succeed: otherwise, the function does not return. File C2A8E1_Merge And Display.cpp must contain a function named MergeAndDisplay. MergeAndDisplay syntax: void MergeAndDisplay(ifstream files[], size_t count); Parameters: files- a pointer to the first element in an array of ifstream objects having count elements, where each object represents a text file open in the text mode for reading. count - the number of elements in the array in files Synopsis: Proceeding in order from the first file specified in files, the first line in each file is read and displayed, followed by the second line in each, followed by the third, etc. When the end of any file is reached that file is closed and the process continues using only the remaining open files until all files have finally been closed. Empty lines are displayed as empty lines. Empty files are simply closed and ignored. Return: void 1. Do not attempt to use data you "tried to read from a file before verifying the read was successful. 2. Functions OpenFiles and MergeAndDisplay must be able to handle any number of files specified by their count parameter. 3. You may assume that no line in a file will contain more than 511 characters. 4. Do not display anything other than the exact contents of the files, i.e., no file names, no line numbers, no extra spaces, no extra blank lines, etc. 5. Do not attempt to store the entire contents of any file at once. 6. Do not attempt to read a file again after reaching its end. 7. Do not attempt to determine the number or lines or bytes in any file. 8. Do not use the peek, seekg, seekp, tellg, or tellp functions. f3 Line 3: Line 4: Manually run your program twice, specifying the names of the instructor-supplied data files listed below on the command line in the order shown. Each must be placed in the program's "working directory". DO NOT prompt the user for the file names or place them in your program code. Command line file names for test 1: mFile 1.txt mFile2.txt mFile 3.txt mFile 4.txt mFile5.txt Command line file names for test 2: mFile 3.txt mFile2.txt mFile 1.txt Example: If the command line specifies files fl f2f3 and those files contain the following text: f1 f2 Line 1: Hello from Bah bah black Now is Line 2: the sheep the other side of time for all good men and Line 5: the universe the display would be as follows, where represents an actual blank line: Hello from Bah bah black Now is the sheep the other side of time for all good men and the universe Hints: C++ does not allow copies to be made of ifstream objects. Dynamically create an array of ifstream objects with one element for each file to open. These objects may also be used in conjunction with the is open function to determine if a file is still open

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions