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
Solved

strtok Problem

Posted on 2002-06-14
1
238 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 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