我正在尝试编写一个程序,它将只读取我的文本文件的第一行,然后将该数字输入到一个 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|错误:没有匹配函数来调用 'getline(std::ifstream&, int&)'|
最佳答案
如果将 getline 替换为:
if (myfile >> highscore)
cout << "Read " << highscore << '\n';
else
cout << "Couldn't read an int\n";
您将能够将 int 读入 highscore。您需要使用 getline 吗?
关于c++ - 从 .txt 文件中读取一行并插入到变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13225116/