在C++中,我试图读取文件并将该文件中的字符串存储到程序中的字符串中。这很好,直到我得到最后一个字,总是存储两次。相同的单词添加两次
ifstream inputStream;
string next = "";
string allMsg = "";
inputStream.open(fileName.c_str());
string x;
while (!inputStream.eof())
{
inputStream >> x;
next = next + " " + x;
}
cout << "The entire message, unparsed, is: " << next << endl;
这样做会增加最后一个字或int从我打开的文件到下一个。有什么建议么?谢谢!
['而(!EOF())'是错误的。(http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside -a-loop-condition-considered-wrong) – chris