C++ 读取/写入输出文件错误(可能是简单修复)

C++ 读取/写入输出文件错误(可能是简单修复),c++,string,variables,int,ifstream,C++,String,Variables,Int,Ifstream,所以我确信这里有很多“低效”的代码,但我只是在学习 我在这里遇到的问题是,当我显示以前的高分时(它以以下格式保存在“score.txt”中): NAME score right wrong 看起来是这样的: Bob 40 2 2 保存分数效果很好,但是,ifstream提取错误的信息。它不显示以前的高分信息(分数=40,右=2,错误=2),而是显示-80883004或类似的数字 有人从下面的代码中看到是什么导致了这种情况吗 void Score(int score, string name,

所以我确信这里有很多“低效”的代码,但我只是在学习

我在这里遇到的问题是,当我显示以前的高分时(它以以下格式保存在“score.txt”中):

NAME
score right wrong
看起来是这样的:

Bob
40 2 2
保存分数效果很好,但是,ifstream提取错误的信息。它不显示以前的高分信息(分数=40,右=2,错误=2),而是显示-80883004或类似的数字

有人从下面的代码中看到是什么导致了这种情况吗

void Score(int score, string name, int qRight, int qWrong)
{
    infile.open("Score.txt");

    string nameHS;
    int scoreHS, rightHS, wrongHS;
    char choice;

    getline(infile, nameHS);
    infile >> scoreHS;
    infile >> rightHS;
    infile >> wrongHS;

    system("CLS");

    cout << "You have completed Trivia!\n\n";
    cout << setw(30) << "Your Score  " << setw(30) << "High Score  " << '\n';
    cout << setw(30) << "--------------" << setw(30) << "--------------" << '\n';
    cout << setw(25) << "| Score: " << setw(3) << score << " |"
        << setw(25) << "| Score: " << setw(3) << scoreHS << " |" << '\n';
    cout << setw(25) << "| Right: " << setw(3) << qRight << " |"
        << setw(25) << "| Right: " << setw(3) << rightHS << " |" << '\n';
    cout << setw(25) << "| Wrong: " << setw(3) << qWrong << " |"
        << setw(25) << "| Wrong: " << setw(3) << wrongHS << " |" << '\n';
    cout << setw(30) << "--------------" << setw(30) << "--------------" << "\n\n";

    if (score > scoreHS)
    {
        cout << "Congratulations! You beat the high score!\n\n";
        cout << "Would you like to save your score?\n";
        cout << "(Y/N): ";
        cin >> choice;

        if (choice == 'y' || choice == 'Y')
            saveScore(score, name, qRight, qWrong);
        else if (choice == 'n' | choice == 'N')
            cout << "\nPlay again soon!\n\n";
        else
            cout << "Invalid option... game save incomplete! Good-Bye!\n\n";
    }
    outfile.close();
}

void saveScore(int score, string name, int qRight, int qWrong)
{
    system("CLS");

    cout << "Your HIGH SCORE has been saved!\n";
    cout << "Good luck next game ....\n\n";

    outfile.open("Score.txt", ofstream::out | ofstream::trunc);

    outfile << name << '\n';
    outfile << score << ' ' << qRight << ' ' << qWrong << '\n';

    outfile.close();
}
void Score(int Score、字符串名称、int qRight、int qerror)
{
infile.open(“Score.txt”);
字符串名称;
整数分数,对,错;
字符选择;
getline(填充,名称);
填充>>分数;
填充>>右侧;
填充>>错误;
系统(“CLS”);

cout我认为您的输入文件没有打开。打开infle后,添加以下检查

bool bIsOpen = infile.is_open();

确保bIsOpen为true。如果设置为false,则可能需要将输入文件放在其他目录中。如果您使用的是Visual Studio,请检查工作目录并将文件放在那里。

问题已解决!我忘记关闭以前打开的文件的所有实例。一定是由于某种原因导致了错误。感谢当IO出错时(或在IO出错之前),是谁帮了忙!

控制任何打开或读取函数的结果是一个很好的实践。为什么要混合使用getline和流提取器?有什么更好的选择吗?请记住,我对这一点不太熟悉…我不完全确定流提取器是什么?你是指getline和Infle>>在一起吗?添加了检查以验证Infle正在打开并且是的,所以仍然不确定问题可能是什么。您的代码在我的计算机上运行。我正在使用Visual Studio 2015。您的开发环境是什么?可能您的输入文件有问题???这很奇怪,我也在使用VS 2015。而且我似乎找不到输入文件有任何问题。我已经手动键入了它,并依赖我的输出以格式化它并替换为使用的数据…..你真的复制/粘贴了我的代码并且运行良好吗?给我一些爱,并在我的帖子上投票:)我至少让你朝着正确的方向前进,这是你的输入文件。我很高兴看到你一切正常。祝你好运!