So for this code I have to set it up so that it reads the first ten lines from a file, the last ten lines from a file, and then displays all lines from a file 10 at a time. My issue is that the first text document it's reading from has 26 lines and the second text document only has 8. The second text document displays correctly with no issue but with the first one it only displays all lines from the file 10 at a time. I'll provide the code below:
.h file
#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..\n";
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()
{
ifstream ifile(filename);
int lines = 0;
string arec;
getline(ifile, arec);
//Check if numrecords are less than 10
//Then print total lines to console
if (numrecords<10)
{
cout << "\n" << filename
<< " ----- First 10 records in file -----\n";
while (!ifile.eof())
{
cout << arec << endl;
getline(ifile, arec);
}
cout << "The file has been displyed\n";
}
else
{
//Check if numrecords are more than 10
//Then pause the program output on concosle
//and pause the output on for each 10 lines
lines = 0;
while (!ifile.eof())
{
//reset lines to zero for lines=10
if (lines == 10)
{
cout << endl;
//pause the output on for each 10 lines
system("pause");
lines = 0;
}
cout << arec << endl;
lines++;
getline(ifile, arec);
}
}
cout << "\n----------------------------------\n";
ifile.close();
}
void Assignment11_FileReader::displayLast10records()
{
ifstream ifile(filename/*.c_str()*/);
int lines = 0;
string arec;
getline(ifile, arec);
//Check if numrecords are less than 10
//Then print total lines to console
if (numrecords<10)
{
cout << "\n" << filename
<< " ----- Last 10 records in file -----\n";
while (!ifile.eof())
{
cout << arec << endl;
getline(ifile, arec);
}
cout << "The file has been displyed\n";
}
else
{
//Check if numrecords are more than 10
//Then pause the program output on concosle
//and pause the output on for each 10 lines
lines = 0;
while (!ifile.eof())
{
//reset lines to zero for lines=10
if (lines == 10)
{
cout << endl;
//pause the output on for each 10 lines
system("pause");
lines = 0;
}
cout << arec << endl;
lines++;
getline(ifile, arec);
}
}
cout << "\n----------------------------------\n";
ifile.close();
}
void Assignment11_FileReader::displayAllRecords()
{
ifstream ifile(filename);
int lines = 0;
string arec;
cout << 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";
ifile.close();
}
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
-------------
Assignment11_A.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.
-------
Assignment11_A.txt: ALL records in the 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 . . .ENTER
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
Press any key to continue . . .ENTER
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.
-----
Assignment11_B.txt : # of records in file = 8
Assignment11_B.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.
-------
Assignment11_B.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.
-------
Assignment11_B.txt: ALL records in the file with line numbers
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.
Like I said above, the Assignment11_B works with no issue, it's the Assignment11_A that has issues.