首先,我要指出代码中的一个问题,您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
}