清除输入缓冲区后未提取字符串流



我在使用字符串流时遇到问题。所以我从 csv 文件中获取令牌并将它们分配给一些变量,但我在读取每个令牌后清除了字符串流 sv,并且在第一个令牌之后它停止工作,我不确定为什么。注释的行"在此处停止工作"是它停止正确提取的位置。在Visual Studio中,即使在插入操作后,sv也是"0x000"。我什至在我的代码中还有另一个循环,可以清除一次并再次插入并且有效。

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
}

>str("")不会更改流的状态。 即,如果流在str("")之前处于 EOF 状态,则在str("")之后它仍将处于相同的状态。 为了清除状态,请使用clear();

sv.str("");
sv.clear();

最新更新