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

90.2K

Verified Solution

Question

Programming

Tail of a File, C++ Program.
write a program that asks the user for the name of a text file. Theprogram should display the last 10 lines, or all lines if less than10. The program should do this using seekg

Here is what I have so far.

#include
#include
#include

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 notopen\" << 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 <       }
   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;
   }
}

Answer & Explanation Solved by verified expert
3.6 Ratings (423 Votes)
Following code opens the file once and closes it once allprocessing is done First the user is asked to enter the file nameUsing object file stream is opened if file open    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students