所以,我有如下代码:变量不更新从istringstream
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <cctype>
using namespace std;
int main(int argc, char *argv[])
{
char c;
ifstream f("test.txt");
char n;
char z;
char o;
int output;
istringstream in;
string line;
while (getline(f, line))
{
in.str(line);
do
{
c = in.get();
}
while (isspace(c));
in.unget();
in >> n >> c >> z >> c >> o >> c >> output;
cout << n << z << o << output << endl;
in.str(string());
}
f.close();
return 0;
}
和文件test.txt包含:
A,B,C,1
B,D,F,1
C,F,E,0
D,B,G,1
E,F,C,0
F,E,D,0
G,F,G,0
每行的文本文件格式是“char,char,char,bool”(我忽略了现在可能有空白的事实)。
当我编译并运行该代码((使用Visual Studio 2010),我得到:
ABC1
ABC1
ABC1
ABC1
ABC1
ABC1
ABC1
很显然,这不是我想要的东西没有人有答案,这是怎么回事的。 ?
添加istringstream内循环的伎俩。使用in.seekg(0);没有。 – 2013-05-02 20:09:42
为什么不简单地调用'clear()'而不是'str(std :: string())'? – 2013-05-02 21:52:01
@vlad由于清除重置错误标志,而不是stringstream的内容。 – 2013-05-02 22:46:12