Question

In: Computer Science

Tail of a File, C++ Program. write a program that asks the user for the name...

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;
   }
}

Solutions

Expert Solution

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:


Related Solutions

Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Write a program that asks the user for a file name. The file contains a series...
Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in the array 5)...
Write a C program that performs the following: Asks the user to enter his full name...
Write a C program that performs the following: Asks the user to enter his full name as one entry. Asks the user to enter his older brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have any older brother). Asks the user to enter his younger brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have...
In a file called LengthSum.java, write a program that: - Asks the user to enter a...
In a file called LengthSum.java, write a program that: - Asks the user to enter a string. - Asks the user to enter a second string. - Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please...
Write a program that prompts the user to enter a file name, then opens the file...
Write a program that prompts the user to enter a file name, then opens the file in text mode and reads names. The file contains one name on each line. The program then compares each name with the name that is at the end of the file in a symmetrical position. For example if the file contains 10 names, the name #1 is compared with name #10, name #2 is compared with name #9, and so on. If you find...
In a file called Conversions.java, write a program that: Asks the user to enter a double...
In a file called Conversions.java, write a program that: Asks the user to enter a double number. Stores that number into a variable called z. Casts variable z into an integer, and stores the result into a variable called z1. Creates a variable z2, and sets it equal to the integer closest to z. Creates a variable z3, and sets it equal to the floor of z. Creates a variable z4, and sets it equal to the ceiling of z....
In a file called FourCapitalizations.java, write a program that: Asks the user to enter a string....
In a file called FourCapitalizations.java, write a program that: Asks the user to enter a string. Prints out four different versions of that string: The original version of the string. The upper-case version of the string. The lower-case version of the string. A version where the character at position 0 capitalized, and all other characters in lower case. For example: if the user enters string "gaNDalF12 !! AB3w", your program output should look EXACTLY like this: Please enter a string:...
Program specifics: Write a C++ program that does the following: a. Asks the user for the...
Program specifics: Write a C++ program that does the following: a. Asks the user for the distance to the pin and the depth of the green (both in yards). (Note: The pin is the hole in the green and the depth is the diameter of the green, assuming it is circular.) b. Asks the user for an integer club number from 2 to 10, where 10 is the pitching wedge (this club lifts the ball out of rough, sand, etc)....
Write a program that: a. Asks the user for their first name using a JOptionPane. b....
Write a program that: a. Asks the user for their first name using a JOptionPane. b. Asks the user for their age using a JOptionPane. C. Asks the user for their last name using a JOptionPane. d. Pops up a dialog with the following information: i. Number of characters in the first & last name combined ii. The full name all-in upper-case letters iii. The full name all-in lower-case letters iv. The first letter of the name v. The age...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT