如何只读入文本文件的第一行?

How to read in only the first line of a text file?

我要打开的文本文件的名称是"map.txt"。我只想将文件的第一行读入控制台。文本文件的第一行是:

E1 346 473 1085 3725 30 30

这是我目前的代码。

ifstream file;

file.open("map.txt");

if (!file) //checks to see if file opens properly
{
    cerr << "Error: Could not find the requested file.";
}
    /******* loop or statement needed to read only first line here?**********/

就像 WhozCraig 在他们的评论中所说的那样,使用 std::stringstd::getline()

ifstream file;

file.open("map.txt");
string line;

if (!file) //checks to see if file opens properly
{
    cerr << "Error: Could not find the requested file.";
}
else
{
    if (getline(file, line)) cout << line; // Get and print the line.
    file.close(); // Remember to close the file.
}