まず、コードの問題を指摘する必要があります。iss
最初の行を読み取ってから呼び出した後は失敗状態にwhile(getline(iss, entry, ';'))
なっているため、すべての行を読み取った後、をリセットする必要がありますstringstream
。失敗状態にある理由は、を呼び出した後、ストリームでファイルの終わりに到達するためですstd:getline(iss, entry, ';'))
。
あなたの質問の場合、簡単なオプションの1つは、何かが読み込まれたかどうかを確認すること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
}