This is the code which put an extra line to the std::string array. I am here trying to print last k lines of file. Any help is appreciated.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//file.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit);
void print_last(unsigned k, string file_name)
{
string *data=new string[k];
unsigned last=0;
ifstream is;
is.open(file_name.c_str(), ios::binary);
if(is.is_open())
{
while(getline(is, data[last])) //while (getline( myfile, line )) // same as: while (getline( myfile, line ).good())
{
last=(last+1)%k;
}
is.close();
for(int i=0;i<k;i++)
{
last=(last+1)%k;
cout<<data[last]<<endl;
}
}
}
int main()
{
unsigned k;
string file_name;
cin>>k>>file_name;
print_last(k, file_name);
}