Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Chapter 13 - Programming Challenge #1, #2 File Previewer Write a program the ask

ID: 3694509 • Letter: C

Question

Chapter 13 - Programming Challenge #1, #2 File Previewer Write a program the 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. File Display Program Write a program the 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 10 lines of output at a time, and then pause. Each time the program pauses, it should wait for the sure to type a key before the next 10 lines are displayed. Additional Specifications Implement the solution in a class called assignment11_FileReader. (You will need to create a .h file and a .cpp file for the class) 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. Here is the tester: #include iostream #include iomanip #include fstream #include string using namespace std; #include "Assignment11_FileReader.h" void display_file(string fname); int main() { display_file("assignment11_A.txt"); display_file("assignment11_B.txt"); return 0; } void display_file(string fname) { Assignment11_FileReader myfile(fname); cout << " " << fname << " : # of records in file = " << myfile.getNumRrecords() <<" "; myfile.displayFirst10records(); myfile.displayLast10records(); myfile.displayAllRecords(); }

Explanation / Answer

// Assignment11_FileReader.h file

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

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

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

// reader.cpp

#include "Assignment11_FileReader.h"

void Assignment11_FileReader::setFilename(string _name)
{
numrecords = 0;
string oneRec;
filename = _name;
ifstream ifile(_name);

if(!ifile)
{
cout << "File open failed..program terminating.. ";
system("pause");
exit(0);
}
getline(ifile, oneRec);
while (!ifile.eof())
{
numrecords++;
getline(ifile, oneRec);
}
ifile.close();
}
int Assignment11_FileReader::getNumRrecords()
{
return numrecords;
}
void Assignment11_FileReader::displayFirst10records()
{
cout <<" " << filename
<< " ----- First 10 records in file ----- ";

// place code here to complete function
}
void Assignment11_FileReader::displayLast10records()
{
cout <<" " << filename
<< "---- Last 10 records in file ---- ";
// place code here to complete function
}
void Assignment11_FileReader::displayAllRecords()
{
ifstream ifile(filename);
int lines=0;
string arec;

cout <<filename << "----- All records in file ------ ";

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

cout << " ---------------------------------- ";
ifile.close();
}
void Assignment11_FileReader::displayAllRecords_with_lineNumbers()
{
ifstream ifile(filename);
int lines=0, linenum=1;
string arec;

cout << filename << "--All records in file with line numbers--- ";

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

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


// tester.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
#include "Assignment11_FileReader.h"

void display_file(string fname);
int main()
{
display_file("assignment11_A.txt");
display_file("assignment11_B.txt");

system("pause");
return 0;
}

void display_file(string fname)
{
Assignment11_FileReader myfile;
int choice;
myfile.setFilename(fname);
cout << " ... " << fname << ". # of records in file = "
<< myfile.getNumRrecords() << " ........... ";

myfile.displayFirst10records();

myfile.displayLast10records();

myfile.displayAllRecords();

myfile.displayAllRecords_with_lineNumbers();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote