我正在尝试解决反向单词问题。我的解决方案有效,甚至跳过了空白行。但是,在读取文件的所有行之后,程序会陷入循环,不断地接受输入。这很令人费解,我觉得这与我的外部 while 循环有关,但我看不出它有什么问题。
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
using namespace std;
int main(int argc, char** argv)
{
stack<string> s;
ifstream in;
in.open(argv[1]);
do
{
do
{
string t;
in >> t;
s.push(t);
} while(in.peek() != '\n');
do
{
cout << s.top();
s.pop();
if(s.size() > 0) cout << " ";
else cout << endl;
} while(s.size() > 0);
} while(in.peek() != -1 || in.fail() || in.eof() || in.bad() );
in.close();
return 0;
}