c++ - .EOF 在读取文件 :FIXXED 时导致段错误

标签 c++

<分区>

当我运行我的程序时,它第一次工作,读取正确的输入并将输出写入我的输出文件,但是一旦我第二次运行它,它就不会写入任何东西,即使它读取了,文件也只是空白一切正确。我似乎无法理解它在哪里搞砸了或如何搞砸了,我真的很想知道。这两个图像是我的第一次和第二次运行,只是在第二次运行后我的输出文件是空白的。

first run

second run

#include<iostream>
#include<iostream>
#include<cstdlib>
#include<time.h>
#include<fstream>
#include<string>
using namespace std;



int main()
{
    int i=0;
    int number; // The correct number
    int guess=0;  // The user's guess
    int numGuesses=0; // The number of times the user has guessed
    string lines;
    string player;
    ifstream ifile;
    ofstream ofile;
    //ofstream myfile;
    //
    string names[10];
    int scores[10];
    ifile.open("high_score.txt");

    string first_last_name;
    string temp;
    int score;
    int index=0;
    string title;
    bool topten=false;


    cout <<"Welcome to the number guessing game. The top 10 best scores so far are: "<<endl;
    while(!ifile.eof())
    {

        //getline(ifile,lines);
        ifile >>first_last_name;
        //cout << first_last_name;
        ifile >> temp;
        first_last_name.append(" ");
        first_last_name.append(temp);
        ifile >> score;
        names[index]=first_last_name;
        scores[index++]=score;

    }
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    for (int i=0; i < 10; i++)
    {
        cout << names[i] << " " << scores[i] <<endl;

    }
    // Seed the random generator.
    srand(static_cast<int> (time(NULL)));

    // Generate a random number between 1 and 100.
    number = (rand() % 100) + 1;

    cout << "Let's play the number guessing game! What is your name? " <<endl; 
    getline(cin,player);
    cout << endl;   
    while(guess!=number)
    {
        cout << "guess the number the computer randomly picked between 1 - 100: ";
        cin >> guess;
        numGuesses++;

        // Check if the user has guessed the correct number.
        // If not, tell him if his guess is too low or too high
        if(number > guess)
        {
            cout << "sorry, your guess is too low" << endl;
        }
        else if(number < guess)
        {
            cout << "sorry, your guess is too high" << endl;
        }
        else
        {
            cout << "You guessed right!!!!"<<endl;
            cout << "You win!!!" << endl;
            break;
        }

    }
       cout << "It took you "<< numGuesses << " guesses "<< player<< endl;
////////////////////////////////////////////////////////////////////////
       if (numGuesses<4)
       {
       cout << "Amazing! Or was it luck" << endl;
       }
       else if(numGuesses<6)
       {
           cout <<"That's a very good score..." <<endl;
       }

       else if (numGuesses<8)
       {
        cout << "That's pretty good but you can do better..." << endl;
       }

       else if ( numGuesses<10)
       {
       cout << "Not too shabby, but not too good either..."<< endl;
       }

       else
       {
       cout << "What a terrible score!..." << endl;
       }

    for(int i=0; i < 10; i++)
    {
        if(numGuesses <= scores[i])
        {
            for( int k=9;k>i;k--)
            {   
                scores[k]=scores[k-1];
                names[k]=names[k-1];
            }

            scores[i]=numGuesses;
            names[i]=player;
            topten=true;
            break;
        }


    }
    if(topten==true)
    {

        cout << "Hey, you made it to the top ten , Congratzzzzzzzz!!!!" <<endl;
    }
    ofile.open("high_score.txt");
    for(int i=0;i<10;i++)
    {
        ofile <<"\t"<< names[i] << " " << scores[i] <<endl;
        ofile << endl;

    }

    return 0;
}

最佳答案

您需要在打开输出文件之前关闭输入文件,因为它们都引用同一个文件。

关闭可以通过调用 ifile.close() 显式完成,也可以通过在打开输出文件之前使 ifile 超出范围来隐式完成。

后者可以这样做:

{
    ifstream ifile;
    ifile.open("high_score.txt");
    // do stuff with ifile
}
ofstream ofile;
ofile.open("high_score.txt");
// do stuff with ofile

关于c++ - .EOF 在读取文件 :FIXXED 时导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48413208/

相关文章:

c++ - 重载ostream运算符c++时出错

c++ - 初学者如何处理用户输入类型不正确引起的异常

C++ 运算符重载 >= 有 2 个不同的返回值

c++ - 模数最接近零

c++ - C++11 decltype 的启发式用法

c++ - 为什么在写入文件时删除空格无法读取文件?

c++ - 有人可以解释一下 openCV 中的 detectMultiScale

c++ - 如何将函数引用传递给参数

c++ - 使用 TCP 从客户端向服务器发送字节的问题

c++ - Qt:创建一个 "svg image button"