我正在尝试从我创建的文本文件中读取最后一件事。
目前该文件包含例如"5 + 3 - 2"
。我正在尝试编写一个函数,当用户按下键盘上的 m 键时,它会显示文件中的最后一个内容,在这种情况下将是2
。
听起来很简单,但我无法让它正常工作。目前我有这个代码。谁能帮我吗?为了进一步说明,我希望显示2
。
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;
}
使用上面的代码,它不会显示任何内容。我假设这是因为我在文件的末尾有NULL
。