In: Computer Science
Tail of a File, C++ Program.
write a program that asks the user for the name of a text file. The
program should display the last 10 lines, or all lines if less than
10. The program should do this using seekg
Here is what I have so far.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class File
{
private:
fstream file;
string name;
public:
int countlines();
void printlines();
};
int File::countlines()
{
int total = 0;
file.open("Text.txt");
if (file)
{
do total = 1
}
while (getline(file, filename))
{
else
cout << "The file did not
open" << endl;
total -= 1;
file.close;
return total;
}
}
void File::printlines()
{
file.open("text.txt");
string data;
int total = countlines();
if (total <= 10)
while (getline(file, data))
{
cout <<
data << endl;
}
return;
if (total > 10)
{
file.seekg(0L, ios::beg);
int position = total - 10;
int linecount = 0
do
{
linecount = 1;
if (position + 1) == linecount)
{
getline(file, data)
while (file.fail(1))
{
cout
<< data << endl;
getline(file, data);
}
}
}
while (getline(file, data));
file.close;
}
}
Following code opens the file once and closes it once all processing is done. First the user is asked to enter the file name. Using object file stream is opened. if file open successfully, lines in file are counted. after counting the file stream reaches to the end of the file and it sets some flag (eofbitflag) which needs to be cleared before using seekg. use seekg to move file stream to beginning of the file. Parse the lines except last 10 line. In the last print last 10 lines.
Complete Code:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class FileTail
{
private:
string fileName; // to hold filename
ifstream file; // file stream
public:
// constructor initializes filename
FileTail(string fname)
{
fileName = fname;
}
// public methods
void openFile();
void closeFile();
int countlines();
void printlines();
};
// open file
void FileTail::openFile()
{
// open input file
file.open(fileName.c_str());
if (!file)
{
// file not opened successfully. display error message
cout << "The file did not open" << endl;
}
}
// close file
void FileTail::closeFile()
{
file.close();
}
// count the lines in the file
int FileTail::countlines()
{
int lineCount = 0; // initialize total line count with 0
string str;
// if file was opened successfully
if (file)
{
// get line by line
while (getline(file, str))
{
// increment the line count
lineCount += 1;
}
}
// rturn the line count
return lineCount;
}
// function prints last 10 lines
void FileTail::printlines()
{
int lineCount = 0;
string line;
if (file)
{
// get the total lines count from the file
lineCount = countlines();
// clear the end of file bit flag
file.clear();
// move the file pointer to beginning of the file
file.seekg(ios::beg);
// if line count is less than or equal to 10, print all
lines
if (lineCount <= 10)
{
// iterate till end of the file
while (getline(file, line))
{
// print one line
cout << line << endl;
}
}
else
{
// lines cout is more than 10. display only last 10 lines
// calculate lines count to skip
int position = lineCount - 10;
// skip the lines not required
while (position--)
{
getline(file, line);
}
// only 10 lines left. now iterate till end of file not
reached
while (getline(file, line))
{
// print one line
cout << line << endl;
}
}
}
}
int main()
{
string fileName;
// prompt user for input file name
cout << "Enter the file name of a text file: ";
cin >> fileName;
// create object and pass the inpuuted filename to
constructor
FileTail file(fileName);
// open the file
file.openFile();
// print last 10 lines
file.printlines();
// close the file
file.closeFile();
return 0;
}
INPUT FILE:
84,0,0,0,20
0,24,0,91,0
0,0,12,0,0
0,94,0,19,0
33,0,0,0,62
84,0,0,0,20
0,24,0,91,0
0,0,12,0,0
0,94,0,19,0
33,0,0,0,62
84,0,0,0,20
0,24,0,91,0
0,0,12,0,0
0,94,0,19,0
33,0,0,0,62
OUTPUT:
INPUT FILE:
OUTPUT: