Help with writing cpp program

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

This is my .cpp that needs to be needs changes.

#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdio> //standard input output library
#include "Lab6_FileReader.h"
/*
Constructor
Read file and determine the number of records
*/
Lab6_FileReader::Lab6_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);
	}

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

	ifile.close();
}
/*
return class private variable holding number of records
*/
int Lab6_FileReader::getNumRrecords()
{
	return numrecords;
}
/*
Display first 10 records with line numbers. If the file has fewer than 10 records,
display all records

*/
void Lab6_FileReader::displayFirst10records() //Complete this function
{
	cout << "\n" << filename << ":  FIRST 10 records in file \n\n";
}
void Lab6_FileReader::displayLast10records()//Complete this function
{
	cout << "\n" << filename << ":  LAST 10 records in file \n\n";
}
void Lab6_FileReader::displayAllRecords()
{
	ifstream ifile(filename);
	int displayed_lines = 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 (displayed_lines % 10 == 0 && displayed_lines > 0)
			system("pause");

		cout << setw(2) << (displayed_lines + 1) << ".\t" << arec << endl;
		displayed_lines++;
	}

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

This is my completed .cpp tester file.

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

void display_file(string fname);
int main()
{
	display_file("Lab6_A.txt");
	display_file("Lab6_B.txt");

	system("pause");
	return 0;
}

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

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

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

This is my completed .h(header) file.

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

using namespace std;
class Lab6_FileReader
{
private:
	string filename;
	int numrecords;
public:
	Lab6_FileReader(string _filename);
	void displayFirst10records();
	void displayLast10records();
	void displayAllRecords();
	int getNumRrecords();
};

Which FreeCodeCamp Challenge?

This isn’t a free code camp challege. Just a program I am having trouble with

Any help would be greatly appreciated.

File Previewer

#include "fpreview.h"
#include <cstdio> //standard input output library
#include <cstring>
#include <fstream>
#include <iostream>
// get numeric_limits
#include <limits>
/*
Constructor
Read file and determine the number of records
*/
Lab6_FileReader::Lab6_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);
  }

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

  ifile.close();
}
/*
return class private variable holding number of records
*/
int Lab6_FileReader::getNumRrecords() { return numrecords; }
/*
Display first 10 records with line numbers. If the file has fewer than 10
records,
display all records

*/
void Lab6_FileReader::displayFirst10records() // Complete this function
{
  cout << "\n" << filename << ":  FIRST 10 records in file \n\n";
  ifstream ifile(filename);

  string line;

  // gets line 10 times or till EOF
  // getline sets eofbit when it reaches end of stream,
  // that's why we need getline before i < 10 in condition,
  // otherwise the loop ends after 10th iteration without checking for end
  for (int i = 0; getline(ifile, line) && i < 10; ++i) {
    cout << line << "\n";
  }

  // if stream reached the end
  if (ifile.eof()) {
    cout << "EOF\n";
  }

  ifile.close();
}
void Lab6_FileReader::displayLast10records() // Complete this function
{
  cout << "\n" << filename << ":  LAST 10 records in file \n\n";
  ifstream ifile(filename);
  int length = numrecords;

  // skip till 10 lines remain
  while (length-- > 10) {
    // ignores characters until '\n'
    ifile.ignore(numeric_limits<streamsize>::max(), '\n');
  }

  // undo last length--
  ++length;
  // for length 9...0
  while (length--) {
    string line;
    getline(ifile, line);
    cout << line << "\n";
  }
}
void Lab6_FileReader::displayAllRecords() {
  ifstream ifile(filename);
  int displayed_lines = 0;
  string arec;

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

  while (getline(ifile, arec)) {
    if (displayed_lines % 10 == 0 && displayed_lines > 0)
      system("pause");

    cout << setw(2) << (displayed_lines + 1) << ".\t" << arec << endl;
    displayed_lines++;
  }

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

It keeps giving me the “file open failed” message whenever i compile.

It looks like your files aren’t found where the program expects them to be or otherwise unopenable. Make sure that you pass valid filenames to your Lab6_FileReader constructor. Files must be reachable from wherever you start your program by those exact paths, e.g. my test.txt and testShort.txt are in the same folder as main.cpp and I compile in that same folder. If all else fails try passing absolute paths.

Here is my full setup just in case:

main.cpp

#include "fpreview.h"
#include <iostream>
#include <sys/stat.h>

void display_file(string fname);
void ask_for_file();

int main() {

  display_file("test.txt");

  std::cout << "\nShort File\n";

  display_file("testShort.txt");

  ask_for_file();
}

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

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

  myfile.displayFirst10records();
  myfile.displayLast10records();
  myfile.displayAllRecords();
}

void ask_for_file() {
  std::cout << "\n================================\n"
	    << "Input filename or press ENTER to quit\n";

  string fname;
  // disable skipping leading whitespace, allows to register lone ENTER
  std::cin >> std::noskipws >> fname;

  if (!fname.empty()) {

    // check if file exists
    struct stat buffer;
    if (stat(fname.c_str(), &buffer) != -1) {
      display_file(fname);
    } else {
      std::cout << fname << "file doesn't exist"
		<< "\n";
    }
  }
}

fpreview.h

#ifndef __FPREVIEW_H_INCLUDED__
#define __FPREVIEW_H_INCLUDED__

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

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

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

#endif

fpreview.cpp

#include "fpreview.h"
#include <cstdio> //standard input output library
#include <cstring>
#include <fstream>
#include <iostream>
// get numeric_limits
#include <limits>

const auto MAX_STREAM_CHARS = numeric_limits<streamsize>::max();

/*
Constructor
Read file and determine the number of records
*/
Lab6_FileReader::Lab6_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);
  }

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

  ifile.close();
}
/*
return class private variable holding number of records
*/
int Lab6_FileReader::getNumRrecords() { return numrecords; }
/*
Display first 10 records with line numbers. If the file has fewer than 10
records,
display all records

*/
void Lab6_FileReader::displayFirst10records() // Complete this function
{
  cout << "\n" << filename << ":  FIRST 10 records in file \n\n";
  ifstream ifile(filename);

  string line;

  // gets line 10 times or till EOF
  // getline sets eofbit when it reaches end of stream,
  // that's why we need getline before i < 10 in condition,
  // otherwise the loop ends after 10th iteration without checking for end
  for (int i = 0; getline(ifile, line) && i < 10; ++i) {
    cout << line << "\n";
  }

  // if stream reached the end
  if (ifile.eof()) {
    cout << "EOF\n";
  }

  ifile.close();
}
void Lab6_FileReader::displayLast10records() // Complete this function
{
  cout << "\n" << filename << ":  LAST 10 records in file \n\n";
  ifstream ifile(filename);
  int length = numrecords;

  // skip till 10 lines remain
  while (length-- > 10) {
    // ignores characters until '\n'
    ifile.ignore(MAX_STREAM_CHARS, '\n');
  }

  // undo last length--
  ++length;
  // for length 9...0
  while (length--) {
    string line;
    getline(ifile, line);
    cout << line << "\n";
  }
}
void Lab6_FileReader::displayAllRecords() {
  ifstream ifile(filename);
  int displayed_lines = 0;
  string arec;

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

  while (getline(ifile, arec)) {
    if (displayed_lines % 10 == 0 && displayed_lines > 0) {
      // system("pause");
      cout << "Press ENTER for next batch" << endl;
      cin.get();
    }

    cout << setw(2) << (displayed_lines + 1) << ".\t" << arec << endl;
    displayed_lines++;
  }

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

Thank you very much for your help!