I'm making this class called filestream, it's supposed to spit out a (txt) file like a stream. I'm done but the thing is that the last entry is given twice. What am I doing wrong here?
filestream.h:
class filestream {
public:
std::string file_location;
std::string get();
explicit filestream(std::string file_location);
bool at_end();
private:
std::stringstream ss;
std::string s;
std::fstream f;
};
filestream.cpp:
filestream::filestream(std::string file_location) : file_location(file_location) {
f.open(file_location);
}
std::string filestream::get() {
if (f.is_open()) {
f >> s;
return s;
} else {
return nullptr;
}
}
bool filestream::at_end() {
return f.eof();
}
test.cpp:
#include "demonic.h"
int main (){
filestream fs("/home/devilish/Desktop/fruit.txt");
while (!fs.at_end()){
std::cout << fs.get() << std::endl;
}
return 0;
}
output:
Apple
Peer
Cherry
Mango
Mango
I tried this class twice but the results are the same, can anyone help me with this?