Reputation: 1001
I'm having trouble with stringstream. So I am getting tokens from a csv file and assigning them to some variables but I am clearing stringstream sv after I read every token and after the very first token it stops working and I am not sure why. The commented line, 'stops working here', is where it stops correctly extracting. In visual studio it says sv is '0x000' even after insert operation. I even have another loop in my code that clears it once and does insertion again and that works.
int reviewid;
int userid;
int rating;
string reviewDate;
getline(reviewReader, dummyLine); // skip first line of input
while (getline(reviewReader, input)) {
stringstream ss, sv; // sv used for type conversions
// using delimeter of commas
// order of input in the file is [ movieid, moviename, pubyear]
// , first toiken is movieid, second token will be moviename
// third token will be pubyear
ss << input; // take in line of input
getline(ss, token, ','); // first token
sv << token; // convert toiken to int
sv >> reviewid;
getline(ss, token, ','); //
sv.str("");
sv << token; // STOPS WORKING HERE
sv >> movieid;
sv.str(""); // clear buffer
getline(ss, token, ',');
sv << token;
sv >> userid;
sv.str("");
getline(ss, token, ',');
sv << token;
sv >> rating;
sv.str("");
getline(ss, token, ',');
sv << token;
sv >> reviewDate;
sv.str("");
Review r = Review(reviewid, movieid, userid, rating, reviewDate); // make review object
reviews.push_back(r); // add it to vector of reviews
}
Upvotes: 1
Views: 84
Reputation: 758
str("")
does not change the state of your stream.
i.e. if the stream was in eof state before str("")
it will still be in the same state after str("")
.
In order to clear the state please use clear();
sv.str("");
sv.clear();
Upvotes: 1