Solved

strtok() woes.... How to separate out when two delims are right next to each other?

Posted on 2009-05-07
8
494 Views
Last Modified: 2012-05-06
Hi all,

I am having problems with strtok(). What I want is to get back an empty char when two delims are right next to each other. See my code for example..

I want:
paramPointers[0] = "toaster"
paramPointers[1] = "oven"
paramPointers[2] = "sink"
paramPointers[3] = ""
paramPointers[4] = ""
paramPointers[5] = "television"
paramPointers[6] = "stove"
paramPointers[7] = "fridge"

but what I am getting is:

paramPointers[0] = "toaster"
paramPointers[1] = "oven"
paramPointers[2] = "sink"
paramPointers[3] = "television"
paramPointers[4] = "stove"
paramPointers[5] = "fridge"
paramPointers[6] = nil
paramPointers[7] = nil
char delims[5] = "";
	strcpy(delims, "|");
 
	strcpy(receiveResponse, "toaster|oven|sink|||television|stove|fridge");
	
	receiveTok = strtok(receiveResponse, delims);
	
	while(receiveTok != NULL && iToken < MAX_PARAMS)
	{
		paramPointers[iToken] = receiveTok;
		
		iToken++;
		receiveTok = strtok (NULL, delims);
	}

Open in new window

0
Comment
Question by:ehensens
  • 4
  • 2
  • 2
8 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 24326164
strtok already treats multiple separator characters as one. So, I'm not sure what the question is.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 250 total points
ID: 24326165
If you read the description for how strtok works I think you'll see why this isn't doing what you want.

"To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token."

http://www.cplusplus.com/reference/clibrary/cstring/strtok/
0
 

Author Comment

by:ehensens
ID: 24326193
Hi evilrix,

So are you confirming that my code as is will not do what I want, just by the nature of strtok(), i.e. "||" will always be seen as "|"?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 250 total points
ID: 24326217
Ah, sorry, you DON'T want it to treat multiple separators as one.

Then you can't use strok :) You'll have to write your own tokenizer for that.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24326242
Try this, it should do what you are looking to do with strtok.
#include <string>
#include <vector>
#include <sstream>
 
#define MAX_PARAMS 25
 
int main()
{
	std::string receiveResponse = "toaster|oven|sink|||television|stove|fridge";
	std::vector<std::string> params;
 
	std::stringstream ss(receiveResponse);
	std::string s;
 
	while(std::getline(ss, s, '|'))
	{
		params.push_back(s);
	}
}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 24326263
^^^ I say should because I didn't test it very well :)
0
 

Author Comment

by:ehensens
ID: 24326325
So, then how would I get from params to a type:

    char* parameters[MAX_PARAMS];


0
 
LVL 40

Expert Comment

by:evilrix
ID: 24326460
You don't... you use vector. There is no need to use the unsafe fixed array construct in C++.
http://www.cplusplus.com/reference/stl/vector/

If you need something that can be used in place of char* parameters[MAX_PARAMS]; you can use

char* parameters = &params[0];

However, please note this is only valid as long as no mutating functions (non-const) are called on vector and you do NOT delete[] on this pointer as it is managed by vector.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sameEnds challenge 3 162
Unresolved External Symbols 3 86
Global Keyboard Hooks Blocked 4 77
Raspberry Pi 3 to send text message 9 116
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

832 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