473,503 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing tab separated file

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::stringtokens;
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

Jan 31 '07 #1
1 3402
On 30 Jan 2007 21:26:51 -0800, "Cliff Martin"
<cl**********@gmail.comwrote:
>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

Jan 31 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1485
by: Nafai | last post by:
Hello! I'd be very grateful if you can give some advice to me. I explain my problem: I want to read a file whose structure I need to be like this: #comments......
3
2246
by: Natalia DeBow | last post by:
Hi there, I have another question for .NET RegEx experts. I am reading in a C Sharp file line by line and I am trying to detect comments that start with either // of ///. What I am...
8
2932
by: Eric Anderson | last post by:
I have some files that sit on a FTP server. These files contain data stored in a tab-separated format. I need to download these files and insert/update them in a MySQL database. My current basic...
0
1202
by: Robert Bevington | last post by:
Hi everyone, I've written some code that parses an XML file. The whole thing works correctly, but it's really slow. My example only has two elem_x elements. A normal XML file could theoretically...
4
1328
by: Max | last post by:
I am writing a XML parser in Javascript and i have difficulty to understand the specifics of some "entities". I would want to understand the way in which the non-validating XML Parsers manage...
3
2530
by: Chris | last post by:
Hi everyone, I'm trying to parse through the contents of some text files with regular expressions, but am new to regular expressions and how to use them in VB.net. I'm pretty sure that the...
3
3262
by: Rav | last post by:
hi, i need a function, preferably a program for parsing the complex declarations in C. Can anyone help me in this regard...i appreciate. i work on Turbo C++ 3.0 in windows environment. Also, plz...
4
3906
by: R Wood | last post by:
Greetings - A recent Perl experiment hasn't turned out so well, which has piqued my interest in Python. The project is this: take a Vcard file exported from Apple's Addressbook and use a...
4
1526
by: egonslokar | last post by:
Hello Python Community, It'd be great if someone could provide guidance or sample code for accomplishing the following: I have a single unicode file that has descriptions of hundreds of...
6
1661
by: Damien87 | last post by:
Hi, I have a .data file which is essentially just a text file with a bunch of numbers. It's in the format "xx yy zzz abc ccf fffd sss xxx xx sss qq" Basically it is a random amount of numbers...
0
7192
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.