I'm trying to read the last thing from a text file I've created.
Currently the file contains for e.g "5 + 3 - 2"
. I'm trying to write a function for when the user presses the m key on the keyboard, it displays the last thing in the file, which in this scenario would be 2
.
It sounds simple but I just can't get it to work properly. Currently I have this code. Can anyone help me out? For further clarity, I want the 2
to be displayed.
void DisplayLast(string input) { // FUNCTION FOR IF USER PRESSES M
string temp;
ifstream fileInp("About.txt", ios::in);
if(!fileInp) {
cout << "Failed to open" << endl;
}
while(!fileInp.eof()) {
getline(fileInp, temp);
}
cout << "Last: " << temp << endl;
}
With this code above, it doesn't display anything. I'm assuming that this is because I'm at the end of the file which has NULL
.