从.txt文件中读取一行,然后插入变量

Read one line from .txt file and insert into variable

本文关键字:一行 然后 变量 插入 文件 txt 读取      更新时间:2023-10-16

我正在尝试编写一个程序,该程序将仅读取我的文本文件的第一行,然后将该数字输入到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&)'|

如果将getline替换为:

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

您将能够读取INT进入HighScore。您是否需要使用getline?

相关文章: