0

テキストファイルの最初の行だけを読み取り、その番号を int 変数に入力するプログラムを作成しようとしています。しかし、私はそれを行う方法について混乱しています。

    int highscore; // Starting highscore

  ifstream myfile ("highscore.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline(myfile,highscore);
      cout << highscore << endl;
    }
    myfile.close();
  }

しかし、何らかの理由でエラーが発生します。|25|error: no matching function for call to 'getline(std::ifstream&, int&)'|

4

1 に答える 1

1

getline を次のように置き換えた場合:

if (myfile >> highscore)
    cout << "Read " << highscore << '\n';
else
    cout << "Couldn't read an int\n";

int をハイスコアに読み込むことができます。getline を使用する必要がありますか?

于 2012-11-05T02:16:41.653 に答える