Hello guys!
I have to create a program that prints the last 10 lines of a text or all of it if the number of the lines is <=10. The input will be a file that is unknown to me. This is what I've done so far.
#include <iostream>
#include <fstream>
#include <string>
#define LinesToPrint 10
using namespace std;
int main()
{
ofstream outputfile;
string line;
int lines=0,i=0;
char c;
outputfile.open("newfile.txt");
while (getline(cin,line)) //I copy each line of the file to the file I created
{
outputfile<<line<<endl; //I put endl because I don't know if '\n' is
lines++; //included in getline
}
if (lines==0)
{
cout<<"Den upirxe eisodos!"<<endl; //if the file was empty I print a
} //message saying that
else if (lines<=10)
{
cout<<outputfile<<endl; //if there are 10 lines or less I print it
}
else
{
while (outputfile.get(c)) //my problem is here!!! I also tried to use
{ //getline but I had the same problem as now
if ((c=='\n')&&(i!=lines-LinesToPrint+1))
{
i++;
}
if (i==lines-LinesToPrint)
{
i++;
continue;
}
if (i>lines-LinesToPrint)
{
cout<<c;
}
}
}
outputfile.close();
return 0;
}
if my program entered else, I would want to print the last 10 lines but it seems I can't use ofstream file as input. the error I get when I try to compile is this:
./seira2/ask1.cpp: In function 'int main()':
./seira2/ask1.cpp:30: error: 'struct std::ofstream' has no member named 'get'
can you please help me with that? I am looking for hours to find a solution but I have a deadline and I can't fix it!
Thank you in advance!