Pring Last K Line
/***
13.1
Write a method to print the last K lines of an input file using C++.
IDEA: queue<string> str(K)
***/
#include<iostream>
#include<fstream>
#include<queue>
#include<string>
using namespace std;
#define K 5
queue<string> getlastKlines(char* file) {
queue<string> out;
ifstream f;
string s;
int i = 0;
f.open(file);
while(f.good()) {
getline(f, s);
if(i< K){
out.push(s);
} else {
out.push(s);
out.pop();;
}
i++;
}
return out;
}
void print(queue<string> str) {
while(!str.empty()) {
cout<<str.front()<<endl;
str.pop();
}
}
Comments
Post a Comment