Question

The purpose of this assignment is to develop solutions that perform File IO and objects. Problem specifications are shown below with my changes in blue.

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 10 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 10 24 lines are displayed.

Additional Specifications

  • Implement the solution in a class called asn10_FileReader.

  • This class takes in the name of a text file via it's constructor

  • It provides methods to read and display all the records in the file with line numbers.    

Refer to the Programming Standards document and follow the rules stated there in order to complete the program.

This problem is implemented using classes. The UML diagram for this class is shown below.

Asn10 FileReader Class Fields filename : string numrecords: int Methods displayAlIRecords0:void displayFirst10records0:void -

The class contains a constructors which sets the name of the file to be processed. The constructor also determines the number of records in the file, which it stores in the class variable called numrecords .

In each of the display.. functions, the file with the stored filename is displayed in the specified format.

Assignment 10 incomplete files

This lab is composed of 3 files. These are:

D References Header Files Dasn10_FileReader.h Resource Files Source Files D asn10_FileReader.cpp d maya tolappa_asn10 tester.

The class .h file and the tester file are complete. You need to complete the class .cpp files.

asn10_FileReader.h - No changes needed

#pragma once
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

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

public:
        Asn10_FileReader(string _filename);
        void displayFirst10records();
        void displayLast10records();
        void displayAllRecords();
        int getNumRrecords();
};

asn10_FileReader.cpp - Changed needed!

#include "asn10_FileReader.h"
/*
        Constructor
        Read file and determine the number of records
*/
Asn10_FileReader::Asn10_FileReader(string _name)
{
        numrecords = 0;
        filename = _name;

        string oneRec;
        ifstream ifile(_name);

        if(!ifile)
        {
                cout << _name << ". File open failed..program terminating..\n";
                system("pause");
                exit(0);
        }

        while (getline(ifile, oneRec))
                numrecords++;

        ifile.close();
}
/*
return class private variable holding number of records

*/
int Asn10_FileReader::getNumRrecords()
{
        return numrecords;
}
/*
        Display first 10 records with line numbers. If the file has fewer than 10 records,
        display all records

*/
void Asn10_FileReader::displayFirst10records()
{
        ifstream ifile(filename);


        cout <<"\n" << filename << ":  FIRST 10 records in file \n\n";

        cout << "\n-----------------------------------------\n\n";
}
void Asn10_FileReader::displayLast10records()
{
        ifstream ifile(filename);

        cout << "\n" << filename << ":  LAST 10 records in file \n\n";

        cout << "\n-----------------------------------------\n\n";
}
void Asn10_FileReader::displayAllRecords()
{
        ifstream ifile(filename);
        int lines=0, linenum = 1;
        string arec;

        cout <<"\n" << filename << ": ALL records in the file with line numbers, 10 at a time\n\n";

        while (getline(ifile, arec))
        {
                if(lines >= 10)
                {
                        cout << endl;
                        system("pause");
                        lines = 0;
                }
                cout << setw(2) << linenum << ".\t" << arec << endl;
                linenum++;
                lines++;
        }

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

Complete the highlighted functions.

maya_tolappa_asn10_tester.cpp - No changes needed

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
#include "asn10_FileReader.h"

void display_file(string fname);
int main()
{
        display_file("asn10_Long_file.txt");
        display_file("asn10_Short_file.txt");

        system("pause");
        return 0;
}

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

        cout << "\n" << fname << " :  # of records in file = "
                << myfile.getNumRrecords() <<"\n";

        myfile.displayFirst10records();

        myfile.displayLast10records();

        myfile.displayAllRecords();
        system("pause");
}

Tasks to be completed

  • Download asn10 incomplete files from BlackBoard. Alternatively, you may copy code from this document

  • Rename the class tester files as per the Programming Standards document . Leave the class cpp and .h file unchanged

  • Complete highlighted code as per instructions in them.

  • Refer to the Programming Standards document and follow the rules stated there in order to complete the program.

There are 2 data files provided for you in order to complete this assignment. They are called

  • asn10_Long_file.txt

  • asn10_Short_file.txt.txt

The first file file contains 26 records and the second file contains 8 records. The contents of these files were extracted from:

My output is as follows. Places where I pressed the "Enter" Key is shown highlighted in yellow.

asn10_Long_file.txt : # of records in file = 26

asn10_Long_file.txt: FIRST 10 records in file

1.     A programming language is an artificial language designed

2.     to communicate instructions to a machine, particularly

3.     a computer. Programming languages can be used to create

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

5.     express algorithms precisely.

6.     The earliest programming languages predate the invention

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

8.     machines such as Jacquard looms and player pianos.

9.     Thousands of different programming languages have been

10.     created, mainly in the computer field, with many being

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

asn10_Long_file.txt: LAST 10 records in file

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

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

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

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

21.     dominant implementation that is used as a reference.

22.     The term computer language is sometimes used interchangeably

23.     with programming language.However, the usage of both

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

25.     One usage describes programming languages as a subset

26.     of computer languages.

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

asn10_Long_file.txt: ALL records in the file with line numbers, 10 at a time

1.     A programming language is an artificial language designed

2.     to communicate instructions to a machine, particularly

3.     a computer. Programming languages can be used to create

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

5.     express algorithms precisely.

6.     The earliest programming languages predate the invention

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

8.     machines such as Jacquard looms and player pianos.

9.     Thousands of different programming languages have been

10.     created, mainly in the computer field, with many being

Press any key to continue . . .↩

11.     created every year. Most programming languages describe

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

13.     of commands, although some languages, such as those that

14.     support functional programming or logic programming, use

15.     alternative forms of description.

16.     The description of a programming language is usually split

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

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

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

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

Press any key to continue . . .↩

21.     dominant implementation that is used as a reference.

22.     The term computer language is sometimes used interchangeably

23.     with programming language.However, the usage of both

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

25.     One usage describes programming languages as a subset

26.     of computer languages.

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

Press any key to continue . . .↩

asn10_Short_file.txt : # of records in file = 8

asn10_Short_file.txt: FIRST 10 records in file

1.     This introductory course in C++ programming

2.     includes object-oriented, event-driven,

3.     interactive programming techniques. Topics include

4.     data types, pointers, arrays, stacks, recursion,

5.     string processing, searching and sorting

6.     algorithms, classes and objects, references

7.     and memory addresses, scope, streams and files,

8.     and graphics.

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

asn10_Short_file.txt: LAST 10 records in file

1.     This introductory course in C++ programming

2.     includes object-oriented, event-driven,

3.     interactive programming techniques. Topics include

4.     data types, pointers, arrays, stacks, recursion,

5.     string processing, searching and sorting

6.     algorithms, classes and objects, references

7.     and memory addresses, scope, streams and files,

8.     and graphics.

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

asn10_Short_file.txt: ALL records in the file with line numbers, 10 at a time

1.     This introductory course in C++ programming

2.     includes object-oriented, event-driven,

3.     interactive programming techniques. Topics include

4.     data types, pointers, arrays, stacks, recursion,

5.     string processing, searching and sorting

6.     algorithms, classes and objects, references

7.     and memory addresses, scope, streams and files,

8.     and graphics.

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

Press any key to continue . . .↩

Asn10 FileReader Class Fields filename : string numrecords: int Methods displayAlIRecords0:void displayFirst10records0:void - displayLast10records0: void getNumRrecords0:int
D References Header Files Dasn10_FileReader.h Resource Files Source Files D asn10_FileReader.cpp d maya tolappa_asn10 tester.cpp
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// asn10_FileReader.h

#pragma once

#include <iostream>

#include <fstream>

#include <iomanip>

#include <string>

using namespace std;

class Asn10_FileReader

{

private:

        string filename;

        int numrecords;

public:

        Asn10_FileReader(string _filename);

        void displayFirst10records();

        void displayLast10records();

        void displayAllRecords();

        int getNumRrecords();

};

//end of asn10_FileReader.h

// asn10_FileReader.cpp

#include "asn10_FileReader.h"

#include <cstdlib>

/*

        Constructor

        Read file and determine the number of records

*/

Asn10_FileReader::Asn10_FileReader(string _name)

{

        numrecords = 0;

        filename = _name;

        string oneRec;

        ifstream ifile(_name.c_str());

        if(!ifile)

        {

                cout << _name << ". File open failed..program terminating..\n";

                system("pause");

                exit(0);

        }

        while (getline(ifile, oneRec))

                numrecords++;

        ifile.close();

}

/*

return class private variable holding number of records

*/

int Asn10_FileReader::getNumRrecords()

{

        return numrecords;

}

/*

        Display first 10 records with line numbers. If the file has fewer than 10 records,

        display all records

*/

void Asn10_FileReader::displayFirst10records()

{

        ifstream ifile(filename.c_str());

        int linenum = 0;

        cout <<"\n" << filename << ": FIRST 10 records in file \n\n";

        string arec;

        while(!ifile.eof())

        {

               linenum++;

               getline(ifile,arec);

               if(linenum > 10)

                              break;

               cout << setw(2) << linenum << ".\t" << arec << endl;

        }

        cout << "\n-----------------------------------------\n\n";

        ifile.close();

}

void Asn10_FileReader::displayLast10records()

{

        ifstream ifile(filename.c_str());

        int start_line = numrecords-10;

        if(start_line < 0)

               start_line = 0;

        int linenum = 0;

        string arec;

        cout << "\n" << filename << ": LAST 10 records in file \n\n";

        while(!ifile.eof())

        {

               linenum++;

               getline(ifile,arec);

               if(linenum > start_line)

               {

                              cout << setw(2) << linenum << ".\t" << arec << endl;

               }

        }

        cout << "\n-----------------------------------------\n\n";

        ifile.close();

}

void Asn10_FileReader::displayAllRecords()

{

        ifstream ifile(filename.c_str());

        int lines=0, linenum = 1;

        string arec;

        cout <<"\n" << filename << ": ALL records in the file with line numbers, 10 at a time\n\n";

        while (getline(ifile, arec))

        {

                if(lines >= 10)

                {

                        cout << endl;

                        system("pause");

                        lines = 0;

                }

                cout << setw(2) << linenum << ".\t" << arec << endl;

                linenum++;

                lines++;

        }

        cout << "\n\n-----------------------------------------\n\n";

        ifile.close();

}

//end of asn10_FileReader.cpp

// maya_tolappa_asn10_tester.cpp

#include <iostream>

#include <iomanip>

#include <fstream>

#include <string>

using namespace std;

#include "asn10_FileReader.h"

void display_file(string fname);

int main()

{

        display_file("asn10_Long_file.txt");

        display_file("asn10_Short_file.txt");

        system("pause");

        return 0;

}

void display_file(string fname)

{

        Asn10_FileReader myfile(fname);

        cout << "\n" << fname << " : # of records in file = "

                << myfile.getNumRrecords() <<"\n";

        myfile.displayFirst10records();

        myfile.displayLast10records();

        myfile.displayAllRecords();

       system("pause");

}

//end of maya_tolappa_asn10_tester.cpp

Output:

asn10-Long-file.txt : # of records in file 26 asn10_Long file.txt FIRST 10 records in file A programming language is an artif

asn10_Short_file.txt FIRST 10 records in file This introductory course in C++ programming includes object-oriented, event-dri

Add a comment
Know the answer?
Add Answer to:
The purpose of this assignment is to develop solutions that perform File IO and objects. Problem specifications are shown below with my changes in blue. 1. File Previewer Write a program that asks...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program to display workers schedule for a company. To accomplish this (1) Write...

    Write a C++ program to display workers schedule for a company. To accomplish this (1) Write a struct Time with attributes (at least) : hours, minutes and seconds. Add other functions or attributes you need to get the job done (2) Write a class Schedule with attributes : Day of week, start_time, end_time and activity. Schedule should have at least (a) Display to display the schedule (b) Length to return the duration of the schedule (ie end_time - start_time), (c)...

  • Star Database Program. This is a more sophisticated assignment, it will take longer to do. Write...

    Star Database Program. This is a more sophisticated assignment, it will take longer to do. Write a program which uses file IO, loops, and function calls. The following files are provided in pub: Assignment.cpp stars.dat solution_stars.dat solution.o when you log in the first time, make a directory for this project mkdir prog3 then copy down the files. (WARNING! This will overwrite any file in the directory named Assignment.cpp, so don't copy down Assignment.cpp if you have already done work): cp...

  • Microsoft Visual Studios 2017 Write a C++ program that computes a student’s grade for an assignment...

    Microsoft Visual Studios 2017 Write a C++ program that computes a student’s grade for an assignment as a percentage given the student’s score and total points. The final score must be rounded up to the nearest whole value using the ceil function in the <cmath> header file. You must also display the floating-point result up to 5 decimal places. The input to the program must come from a file containing a single line with the score and total separated by...

  • Develop a functional flowchart and then write a C++ program to solve the following problem. 1....

    Develop a functional flowchart and then write a C++ program to solve the following problem. 1. Create a text file named c1.txt and write your brand of computer (like Dell, HP, etc) in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your program will also read the brand of your computer from the keyboard. The process of the file creation (name of the file, mode for opening...

  • My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all th...

    My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...

  • In c programming . Part A: Writing into a Sequential File Write a C program called...

    In c programming . Part A: Writing into a Sequential File Write a C program called "Lab5A.c" to prompt the user and store 5 student records into a file called "stdInfo.txt". This "stdInfo.txt" file will also be used in the second part of this laboratory exercise The format of the file would look like this sample (excluding the first line) ID FIRSTNAME LASTNAME GPA YEAR 10 jack drell 64.5 2018 20 mina alam 92.3 2016 40 abed alie 54.0 2017...

  • Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include #include using namespace std;...

    Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include #include using namespace std; class Server { public:    Server();    Server(string, int);    ~Server();    string getPiece(int); private:    string *ascii;    mutex access; }; #endif -------------------------------------------------------------------------------------------------------------------------- Server.cpp #include "Server.h" #include #include Server::Server(){} Server::Server(string filename, int threads) {    vector loaded;    ascii = new string[threads];    ifstream in;    string line;    in.open(filename);    if (!in.is_open())    {        cout << "Could not open file...

  • Please provide original Answer, I can not turn in the same as my classmate. thanks In...

    Please provide original Answer, I can not turn in the same as my classmate. thanks In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a “next” pointer. Textbook Type Attribute String title String author String ISBN Textbook* next Textbook Type Attribute String title String...

  • Using an object-oriented approach, write a program to read a file containing movie data (Tile, Director,...

    Using an object-oriented approach, write a program to read a file containing movie data (Tile, Director, Genre, Year Released, Running Time) into a vector of structures and perform the following operations: Search for a specific title Search for movies by a specific director Search for movies by a specific genre Search for movies released in a certain time period Search for movies within a range for running time Display the movies on the screen in a nice, easy to read...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT