Starting from:

$25

Assignment 7

Introduction
The purpose of this assignment is to provide you with experience in creating C++ applications that perform file I-O operations. This problem is not from the book.

Develop a class called instructor_file_processor. This class read a file and displays the contents of the file. The tester program will create 3 instances of the object to display contents of the following 3 files:


file_MayaTolappa.txt
file_AmyChaaban.txt
file_TimMoriarty.txt
Maya-Tolappa

CIS115

CIS120

CIS130

CIS101
Amy-Chaaban

CIS110

CIS185

WEB110

WEB230

WEB230

WEB255
Tim-Moriarty

CIS115

CIS136

CIS150

CIS250

CIS296


The UML diagram of the class is as follows:







Constructor function:

The class constructor takes in the name of the file to be processed and stored it in the class private variable

print function

In the print function, the file is opened and the first record read from the file and printed out. The remaining records from the file are read and printed out. A running counter of number of records read is maintained and printed out after the file has been processed.



The pseudo-code for the constructor is shown above. (it is not complete. I have provided ideas on how the constructor is to be implemented)

Other functions:

I have chosen not to implement accessor and mutator function in order to keep the class simple.



Implement the class in 2 files. This will result in your solution containing 3 files - class .h, class .cpp and class tester cpp file. Use my class tester file. The solution looks like this:







asn7_class_tester.cpp file
#include <iostream
using namespace std;
#include "instructor_file_processor.h"

int main()
{
instructor_file_processor inst_maya("file_MayaTolappa.txt");
instructor_file_processor inst_amy("file_AmyChaaban.txt");
instructor_file_processor inst_tim("file_TimMoriarty.txt");

inst_maya.print();
inst_amy.print();
inst_tim.print();

return 0;
}




Output from my implementation of the assignment
Name of datafile : file_MayaTolappa.txt

Name of Instructor : Maya-Tolappa

1. CIS115

2. CIS120

3. CIS130

4. CIS101

# of classes taught : 4

--------------------------------



Name of datafile : file_AmyChaaban.txt

Name of Instructor : Amy-Chaaban

1. CIS110

2. CIS185

3. WEB110

4. WEB230

5. WEB230

6. WEB255

# of classes taught : 6

--------------------------------



Name of datafile : file_TimMoriarty.txt

Name of Instructor : Tim-Moriarty

1. CIS115

2. CIS136

3. CIS150

4. CIS250

5. CIS296

# of classes taught : 5

--------------------------------



Press any key to continue . . .


Submit Instructions
When developing the assignment program, please refer to the programming standards document. The link to this file is as follows:

● Programming Standards Document



Your program should form to the rules specified in Sections I, II , III and IV. Failure to do so will result in 0 points for the assignment. Rather than create function names with get as the start phrase, use the function names suggested by the problem specs.



Turn in one of the following via BlackBoard's submission tool.

● Zip file with the assignment .h and .cpp files

● Zipped version of the solution folder created by Visual Studio



Sample program for this week
Sample programs do not have to be turned in for points. However, it is a good idea to key them and see if they work correctly.



The sample program for the week is a combination of the following programs written as 1 program with 3 functions:

● Problem #1, page 911

● Problem #2, page 911

● Problem #4, page 911.



Problem descriptions are as follows:



1. File Previewer

Write a program that asks the user for the name of a text file. The program should display the first 10 lines of the file on the screen. If the file has fewer than 10 lines, the entire file should be displayed along with a message indicating the entire file has been displayed.



2. File Display Program

Write a program that asks the user for the name of a file. The program should display the contents of the file on the screen. If the file’s contents won’t fit on a single screen, the program should display 24 19 lines of output at a time, and then pause. Each time the program pauses, it should wait for the user to type a key before the next 24 19 lines are displayed.



4. Tail of a File

Write a program that asks the user for the name of a text file. The program should display the last 10 lines of the file on the screen (the “tail” of the file). If the file has less than 10 lines, the entire file is displayed, with a message that the entire file has been displayed. The program should do this by seeking to the end of the file and then backing up to the tenth line from the end.


Additional Specifications

● Implement the solution in a class called week7_sample_FileReader.

● This class takes in the name of a text file via its constructor

● It provides methods to perform file display operations



The UML diagram for this class is shown below.



Files used to test week7_sample_FileReader
I used 2 files to test the week7_sample_FileReader class. These are shown below, in rather small text. Download these from BlackBoard if you wish to work on the sample program. The content was taken from http://en.wikipedia.org/wiki/Programming_language. The current contents of the website are different from the content in the following files.



week7_sample_program_long.txt
week7_sample_program_short.txt
A programming language is an artificial language designed

to communicate instructions to a machine, particularly

a computer. Programming languages can be used to create

programs that control the behavior of a machine and/or to

express algorithms precisely.

The earliest programming languages predate the invention

of the computer, and were used to direct the behavior of

machines such as Jacquard looms and player pianos.

Thousands of different programming languages have been

created, mainly in the computer field, with many being

created every year. Most programming languages describe

computation in an imperative style, i.e., as a sequence

of commands, although some languages, such as those that

support functional programming or logic programming, use

alternative forms of description.

The description of a programming language is usually split

into the two components of syntax (form) and semantics (meaning).

Some languages are defined by a specification document (for example,

the C programming language is specified by an ISO Standard),

while other languages, such as Perl 5 and earlier, have a

dominant implementation that is used as a reference.

The term computer language is sometimes used interchangeably

with programming language.However, the usage of both

terms varies among authors, including the exact scope of each.

One usage describes programming languages as a subset

of computer languages.
This introductory course in C++ programming includes object-oriented,

event-driven, interactive programming techniques. Topics include

data types, pointers, arrays, stacks, recursion, string processing,

searching and sorting algorithms, classes and objects, references

and memory addresses, scope, streams and files, and graphics. A wide

variety of business-oriented problems are solved by writing C++ programs.





week7_sample_FileReader.h
#pragma once
#include <iostream
#include <fstream
#include <iomanip
#include <string

using namespace std;
class week7_sample_FileReader
{
private:
string filename;
int numrecords;

public:
week7_sample_FileReader(string _filename);
void displayFirst10records();
void displayLast10records();
void displayAllRecords();
};


week7_sample_FileReader.cpp


#include "week7_sample_FileReader.h"

week7_sample_FileReader::week7_sample_FileReader(string _name)
{
numrecords = 0;
string oneRec;
filename = _name;
ifstream ifile(_name);

if(!ifile)
{
cout << "File open failed..program terminating..\n";
system("pause");
exit(0);
}
getline(ifile, oneRec);
while (!ifile.eof())
{
numrecords++;
getline(ifile, oneRec);
}
ifile.close();
}

void week7_sample_FileReader::displayFirst10records()
{
ifstream ifile(filename);
int recnum = 0;
string arec;
cout << filename << " has " << numrecords << " records \n";
cout <<"\n" << filename << " ------- First 10 records in file -----\n\n";

getline(ifile, arec);

while (!ifile.eof() && recnum < 10)
{
cout << arec << endl;
getline(ifile, arec);
recnum++;
}
cout << "\n---------------------------------\n\n";
}


void week7_sample_FileReader::displayLast10records()
{
ifstream ifile(filename);
int recToSkip;
string arec;

if(numrecords < 10)
{
recToSkip = 0;
getline(ifile, arec);
}
else
recToSkip = numrecords - 9;

cout << filename << " has " << numrecords << " records \n";
cout <<"\n" << filename << "---- Last 10 records in file -----\n\n";
for(int i=0; i < recToSkip; i++)
{
getline(ifile, arec);
}

while(!ifile.eof())
{
cout << arec << endl;
getline(ifile, arec);
}
ifile.close();
cout << "\n-----------------------------------------\n\n";
}


void week7_sample_FileReader::displayAllRecords()
{
ifstream ifile(filename);
int lines=0;
string arec;

cout << filename << " has " << numrecords << " records \n";
cout <<"\n" << filename << "------- All records in file -----------\n\n";

getline(ifile, arec);
while (!ifile.eof())
{
if(lines = 19)
{
cout << endl;
system("pause");
lines=0;
}
cout << arec << endl;
lines++;
getline(ifile, arec);
}

cout << "\n-----------------------------------------\n\n";
ifile.close();
}



mtolappa_week7_sample_tester.cpp
#include <iostream
#include <iomanip
#include <fstream
#include <string
using namespace std;
#include "week7_sample_FileReader.h"

void display_file(string fname);
int main()
{
display_file("week7_sample_program_long.txt");
display_file("week7_sample_program_short.txt");

return 0;
}

void display_file(string fname)
{
week7_sample_FileReader myfile(fname);

myfile.displayFirst10records();

cout << "Press any key to continue..\n";
cin.get();

myfile.displayLast10records();

cout << "Press any key to continue..\n";
cin.get();

myfile.displayAllRecords();

cout << "Press any key to continue..\n";
cin.get();
}



Output from Sample program execution
The output is shown below. Places where I pressed the enter key upon receiving the "Press any key to continue.." message are shown in blue.



When processing the week7_sample_program_long.txt file, the screen pauses after every 19 records, allowing the user to view the screen before proceeding.




week7_sample_program_long.txt has 26 records



week7_sample_program_long.txt ------- First 10 records in file -----



A programming language is an artificial language designed

to communicate instructions to a machine, particularly

a computer. Programming languages can be used to create

programs that control the behavior of a machine and/or to

express algorithms precisely.

The earliest programming languages predate the invention

of the computer, and were used to direct the behavior of

machines such as Jacquard looms and player pianos.

Thousands of different programming languages have been

created, mainly in the computer field, with many being



---------------------------------



Press any key to continue..



week7_sample_program_long.txt has 26 records



week7_sample_program_long.txt---- Last 10 records in file -----



into the two components of syntax (form) and semantics (meaning).

Some languages are defined by a specification document (for example,

the C programming language is specified by an ISO Standard),

while other languages, such as Perl 5 and earlier, have a

dominant implementation that is used as a reference.

The term computer language is sometimes used interchangeably

with programming language.However, the usage of both

terms varies among authors, including the exact scope of each.

One usage describes programming languages as a subset

of computer languages.



-----------------------------------------



Press any key to continue..



week7_sample_program_long.txt has 26 records



week7_sample_program_long.txt------- All records in file -----------



A programming language is an artificial language designed

to communicate instructions to a machine, particularly

a computer. Programming languages can be used to create

programs that control the behavior of a machine and/or to

express algorithms precisely.

The earliest programming languages predate the invention

of the computer, and were used to direct the behavior of

machines such as Jacquard looms and player pianos.

Thousands of different programming languages have been

created, mainly in the computer field, with many being

created every year. Most programming languages describe

computation in an imperative style, i.e., as a sequence

of commands, although some languages, such as those that

support functional programming or logic programming, use

alternative forms of description.

The description of a programming language is usually split

into the two components of syntax (form) and semantics (meaning).

Some languages are defined by a specification document (for example,

the C programming language is specified by an ISO Standard),



Press any key to continue..



while other languages, such as Perl 5 and earlier, have a

dominant implementation that is used as a reference.

The term computer language is sometimes used interchangeably

with programming language.However, the usage of both

terms varies among authors, including the exact scope of each.

One usage describes programming languages as a subset

of computer languages.



-----------------------------------------



Press any key to continue..



week7_sample_program_short.txt has 6 records



week7_sample_program_short.txt ------- First 10 records in file -----



This introductory course in C++ programming includes object-oriented,

event-driven, interactive programming techniques. Topics include

data types, pointers, arrays, stacks, recursion, string processing,

searching and sorting algorithms, classes and objects, references

and memory addresses, scope, streams and files, and graphics. A wide

variety of business-oriented problems are solved by writing C++ programs.



---------------------------------



Press any key to continue..



week7_sample_program_short.txt has 6 records



week7_sample_program_short.txt---- Last 10 records in file -----



This introductory course in C++ programming includes object-oriented,

event-driven, interactive programming techniques. Topics include

data types, pointers, arrays, stacks, recursion, string processing,

searching and sorting algorithms, classes and objects, references

and memory addresses, scope, streams and files, and graphics. A wide

variety of business-oriented problems are solved by writing C++ programs.



-----------------------------------------



Press any key to continue..



week7_sample_program_short.txt has 6 records



week7_sample_program_short.txt------- All records in file -----------



This introductory course in C++ programming includes object-oriented,

event-driven, interactive programming techniques. Topics include

data types, pointers, arrays, stacks, recursion, string processing,

searching and sorting algorithms, classes and objects, references

and memory addresses, scope, streams and files, and graphics. A wide

variety of business-oriented problems are solved by writing C++ programs.



-----------------------------------------



Press any key to continue..



More products