c++ - Variables Not Updating From istringstream -
so, i've got code following:
#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); { 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; }
and file test.txt contains:
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
the format of each line in text file "char,char,char,bool" (i'm ignoring fact there may whitespace in middle of line, now).
when compile , run code ((using visual studio 2010), get:
abc1 abc1 abc1 abc1 abc1 abc1 abc1
obviously, isn't want. have answer what's going on here?
a quick fix, put istringstream
inside loop reset input indicator:
//istringstream in; ----------+ string line; | while (getline(f, line)) | { | istringstream in; <--------+ in.str(line); { 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()); <-------------------- can remove line } f.close();
if don't reset input indicator, in.get
not work expect. or can use seekg(0)
Comments
Post a Comment