首先,我應該指出代碼存在問題,您的iss
在讀取第一行然後調用while(getline(iss, entry, ';'))
後處於失敗狀態,因此讀完每行後需要重置stringstream
。它處於失敗狀態的原因是在調用std:getline(iss, entry, ';'))
之後在文件流中到達文件末尾。
對於你的問題,一個簡單的選擇是簡單地檢查是否任何被讀入entry
,例如:
stringstream iss;
while(getline(file, line)) {
iss << line; // This line will fail if iss is in fail state
entry = ""; // Clear contents of entry
while(getline(iss, entry, ';')) {
// Do something
}
if(entry == "") // If this is true, nothing was read into entry
{
// Nothing was read into entry so do something
// This doesn't handle other cases though, so you need to think
// about the logic for that
}
iss.clear(); // <-- Need to reset stream after each line
}
我不會用這個'stringstream',只是在分號分開的每一行。 – 2013-03-10 22:16:15
看看[這個問題](http://stackoverflow.com/questions/1120140/csv-parser-in-c?rq=1) – 2013-03-10 22:17:16
我不能使用,它被評估計算機禁止。 –
user2129659
2013-03-10 22:21:31