[英]Read one line from .txt file and insert into variable
我正在尝试编写一个程序,该程序将仅读取文本文件的第一行,然后将该数字输入到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 int\n";
您将能够将一个int读入高分。 您需要使用getline吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.