?
Solved

strtok Problem

Posted on 2002-06-14
1
Medium Priority
?
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 32

Accepted Solution

by:
jhance earned 800 total points
ID: 7077930
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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