i have file number of chars separated spaces:
# cat file.txt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
i trying put them structure, example vector, using ifstream , istringstream in followed way:
string line; ifstream file("file.txt"); vector<char> row while(getline(file, line)){ char a; istringstream iss(line); vector<char> row; while(iss){ iss >> a; row.push_back(a); } table.pushback(row); }
the issue iss return me last element twice in configuration:
for(int i=0; i<table.size(); i++) { for(int j=0; j<table[i].size(); j++){ cout << table[i][j] << " "; } cout <<endl; } . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
i believe issue caused end of line character, , reasons, last char stored.
also when read numbers line "1 2 3 4 5 6 7" program print "1 2 3 4 5 6 7 7"
is particular reason why way of processing file somehow wrong? or maybe missing trivial in example.
statement while(iss)
calls bool
-operator on istringstream
, indicates if failure has occurred in previous read. hence, when last read has been successful, loop entered once more (because no failure has happened far); next read of iss >> a
fail, since ignore return value of operation iss >> a
, (unchanged) value of a
pushed once more vector.
write while(iss >> a)
instead.
Comments
Post a Comment