在我的程序中,用户收集点,并且在程序结束时,如果点大于文件中的最小点数,则将点保存到文件中。由于某种原因,程序无法读取文件,因此最小int的值保持为零。这意味着它仍将写入文件。由于某种原因,程序的写入部分工作,而读取部分则不工作。我已经在代码块中测试了这段代码,它运行得很好,但我不知道为什么它在QT中不起作用。
std::ifstream myFile;
myFile.open("/scoreboard");
largest = 0;
smallest = 0;
myFile >> next;
while(myFile>>next)
{
if(largest<next)
{
largest = next;
}
if(smallest>next)
{
smallest = next;
}
}
if (points>smallest)
{
std::ofstream myfile;
myfile.open ("scoreboard", std::ios::app);
myfile << "\n" << points << std::endl;
myfile.close();
}
if (points>=largest)
{
ui->textBrowser->setText("Your final score is " + QString::number(points) + " points. The highest score is " + QString::number(points) + " points, which was right now!");
}
else
{
ui->textBrowser->setText("Your final score is " + QString::number(points) + " points. The highest score is " + QString::number(largest) + " points.");
}