I am trying to read CSV comma delimited file, content of file are
one,,three
And code to read file is this…
inFile.getline(line, 500);
token1 = strtok(line, ",");
token2 = strtok(NULL, ",");
token3 = strtok(NULL, ",");
if(token1 != NULL){
cout << "token1 = " << token1 << "
";
}else{
cout << "token1 = null
" ;
}
if(token2 != NULL){
cout << "token2 = " << token2 << "
";
}else{
cout << "token2 = null
" ;
}
if(token3 != NULL){
cout << "token3 = " << token3 << "
";
}else{
cout << "token3 = null
";
}
Output is this
token1 = one
token2 = three
token3 = null
Whereas my expectation are that output should be like this…
token1 = one
token2 = null
token3 = three
I did change if statements from
if(token1 != NULL)
To
if(token1)
But it as well doesn’t works.
After checking this example http://www.cplusplus.com/reference/cstring/strtok/, I have updated
token2 = strtok(NULL, ",");
To
token2 = strtok(NULL, ",,");
As well it does not works
See Question&Answers more detail:os