Solved

strtok Problem

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

705 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