Parsing tab separated file

C

Cliff Martin

I know this is popular subject on this group, and I have reviewed a
few messages, but they don't seem to help in my case. Most of the
examples skip repeated separators. I need to read them as a field

I have the following code that's not working:

std::vector<std::string> tokens;
std::string::size_type pos=0, lastpos=0;
std::string tmpstr;

pos = m_record.find_first_of("\t", lastpos);
while (std::string::npos != pos || std::string::npos != lastpos) {
tmpstr = m_record.substr(lastpos, pos - lastpos);
if (tmpstr == "NULL")
tmpstr = "";

tokens.push_back(tmpstr);
lastpos = pos+1;
pos = m_record.find_first_of("\t", lastpos);
std::cout << "Pos: " << pos << " Lastpos: " << lastpos <<
std::endl;
}

I am expecting it to tokenize a string, and store the tokens in a
vector called tokens, including any blank fields (a tab immediately
followed by another tab) Any field that is filled with the text NULL
should be empty instead. I am expecting 36 fields.

It's not working. I have tried a number of little changes here and
there, but I can't get it to exit the while loop. I have put in a few
debug messages, and it prints out what I expect, it just doesn't exit.
I have left one of them.

Here is the output of that cout:

Pos: 1 Lastpos: 0
Pos: 4 Lastpos: 2
Pos: 9 Lastpos: 5
Pos: 25 Lastpos: 10
Pos: 38 Lastpos: 26
Pos: 54 Lastpos: 39
Pos: 59 Lastpos: 55
Pos: 65 Lastpos: 60
Pos: 72 Lastpos: 66
Pos: 77 Lastpos: 73
Pos: 82 Lastpos: 78
Pos: 84 Lastpos: 83
Pos: 89 Lastpos: 85
Pos: 106 Lastpos: 90
Pos: 111 Lastpos: 107
Pos: 123 Lastpos: 112
Pos: 128 Lastpos: 124
Pos: 131 Lastpos: 129
Pos: 134 Lastpos: 132
Pos: 141 Lastpos: 135
Pos: 143 Lastpos: 142
Pos: 147 Lastpos: 144
Pos: 155 Lastpos: 148
Pos: 162 Lastpos: 156
Pos: 167 Lastpos: 163
Pos: 176 Lastpos: 168
Pos: 181 Lastpos: 177
Pos: 186 Lastpos: 182
Pos: 191 Lastpos: 187
Pos: 196 Lastpos: 192
Pos: 198 Lastpos: 197
Pos: 203 Lastpos: 199
Pos: 208 Lastpos: 204
Pos: 213 Lastpos: 209
Pos: 218 Lastpos: 214
Pos: 4294967295 Lastpos: 219 // pos equals std::string::npos on my
machine, so why doesn't it exit?

Any ideas?

Cliff
 
R

rossum

Pos: 4294967295 Lastpos: 219 // pos equals std::string::npos on my
machine, so why doesn't it exit?
Because your while statement does not just check std::string::npos !=
pos, it also checks std::string::npos != lastpos which is true
(lastpos = 219), so (false || true) is true and the loop continues.

rossum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top