Solved

strtok Problem

Posted on 2002-06-14
1
236 Views
Last Modified: 2011-10-03

On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in the string.  Then it puts a NULL in and carries on from where it left off.

Using the this string with delimiters is fine…

WHITE|GREEN|BLUE|YELLOW|ORANGE|PINK|RED|BLACK

I get all the tokens out in order as expected.  However I have to deal with situations where some of the fields are blank, so there will be two fence characters side by side.  And I want to know which fields are empty.

So with a string like this…

|GREEN||YELLOW|ORANGE|PINK||BLACK

I want to get this result…

Field 1:
Field 2: GREEN
Field 3:
Field 4: YELLOW
Field 5: ORANGE
Field 6: PINK
Field 7:
Field 8: BLACK

I know that my records should have 8 fields each.  But I need to know which are empty, even the ones at the start and the end.
0
Comment
Question by:eoincorrigan
1 Comment
 
LVL 32

Accepted Solution

by:
jhance earned 200 total points
Comment Utility
I don't see what your question is here.

strtok does EXACTLY what you are asking about.  If you have "missing" data fields strtok will still find the delimiter but the string you get will be an empty string. In other words, if you check the pointer returned it will be non-NULL but the length of the string (strlen(x)) will be 0.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now