stringstream 清除后不接受任何数据
stringstream didn't accept any data after clear it
我正在尝试从用户那里读取一个以“()”结尾的数据块并将其发送到stringstream,对其进行一些操作(为了方便,现在假设我只想打印这些数据),并再次接受一个新的数据块,通过输入 EOF 完成此操作,
这是代码
while(cin >> temp)
{
if(temp != "()")
{
strem << temp + " ";
}
else
{
while(strem >> str)
cout << str << " ";
cout << "\n";
strem.str("");
}
}
现在在这一行之后 strem.str("");
strem 没有在下一个循环中读取任何数据。我尝试通过添加此行
来调试它
cout << strem.str() << "\n";
所以整个代码是
while(cin >> temp)
{
if(temp != "()")
{
strem << temp + " ";
cout << " strem.str() : " cout << strem.str() << "\n";
}
else
{
while(strem >> str)
cout << str << " ";
cout << "\n";
//strem.str("");
strem.str(string());
}
}
看看strem的内容是什么,总而言之,它打印了第一个块,但之后什么都不打印。知道发生了什么!!
这样做
while(strem >> str)
几乎可以肯定流上设置了标志。所以你需要在尝试再次写入之前清除流状态。
你可以用
strem.clear()
我正在尝试从用户那里读取一个以“()”结尾的数据块并将其发送到stringstream,对其进行一些操作(为了方便,现在假设我只想打印这些数据),并再次接受一个新的数据块,通过输入 EOF 完成此操作, 这是代码
while(cin >> temp)
{
if(temp != "()")
{
strem << temp + " ";
}
else
{
while(strem >> str)
cout << str << " ";
cout << "\n";
strem.str("");
}
}
现在在这一行之后 strem.str("");
strem 没有在下一个循环中读取任何数据。我尝试通过添加此行
cout << strem.str() << "\n";
所以整个代码是
while(cin >> temp)
{
if(temp != "()")
{
strem << temp + " ";
cout << " strem.str() : " cout << strem.str() << "\n";
}
else
{
while(strem >> str)
cout << str << " ";
cout << "\n";
//strem.str("");
strem.str(string());
}
}
看看strem的内容是什么,总而言之,它打印了第一个块,但之后什么都不打印。知道发生了什么!!
这样做
while(strem >> str)
几乎可以肯定流上设置了标志。所以你需要在尝试再次写入之前清除流状态。 你可以用
strem.clear()