Good evening, I've got the following problem. I am parsing csv file like this:
entry1;entry2;entry3
entry4;entry5;entry6
;;
I'm getting entries this way:
stringstream iss;
while(getline(file, string) {
iss << line;
while(getline(iss, entry, ';') {
/do something
}
}
But I've got a problem with last row (;;) where I did read only 2 entries, I need to read the third blank entry. How can I do it?
First, I should point out a problem in the code, your
iss
is in the fail state after reading the first line and then callingwhile(getline(iss, entry, ';'))
, so after reading every line you need to reset thestringstream
. The reason it is in the fail state is that the end of file is reached on the stream after callingstd:getline(iss, entry, ';'))
.For your question, one simple option is to simply check whether anything was read into
entry
, for example: