CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

strtok problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
romanrdgz



Joined: 28 Jan 2011
Posts: 16

View user's profile Send private message

strtok problem
PostPosted: Thu Nov 24, 2011 3:42 am     Reply with quote

I have a string with some fields divided by commas, and finalised by a ;

When I'm specting 2 fields, I'm using:

Code:
void processTokens(char *command){
char separador[3];
cmd[100];

strcpy(cmd, command);
strcpy(separator, ",;");
token = strtok(cmd, separator);
// some stuff... and then i get the second token:
token = strtok(0, separator)
//...
}


When I just have a single field, I was expecting token = strtok(0, separador) to be == NULL. But if I previously had a 2 field msg stored in cmd, the second token will be the previous second token.

So, cmd is not being erased between different executions of the fuction. What's the problem happening here? How can I fix it?[/code]
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Nov 24, 2011 4:33 am     Reply with quote

cmd never gets erased, unless you program it to be erased. It simply gets overwritten each time. This is normal, and should not be a problem.

What's probably happening is that when you just have one token, the strtok(0, separator) call runs over the terminating null of the string, which is now at its search *start* point, and looks for tokens in any old stuff that's lying around after the end of the current string. The help doesn't specify what happens when strtok() encounters a terminating null in the string. this might be a problem with the special strtok(0, ) case.

You are going to have to check for end of string yourself, before calling strtok(). Think about what might happen if you give it a string without ',' or ';'? What will it do with an empty string? Should you perhaps include \0 as a valid separator?

RF Developer
romanrdgz



Joined: 28 Jan 2011
Posts: 16

View user's profile Send private message

PostPosted: Thu Nov 24, 2011 5:14 am     Reply with quote

So, if I declare FILE stream as static, should that do the trick?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Nov 24, 2011 6:04 am     Reply with quote

No, a static declaration will not help your problem.

You didn't post a complete program so we can't be sure of the exact problem, but as I see it there are several possible causes:
1) You have a problem in command, maybe it is not being zero terminated after the first field?
2) There could be a bug in the CCS implementation of strtok. Not very likely as it is an old and well tested function. Post your compiler version number so we can check.

One workaround is to fill cmd with all zeroes before copying:
Code:
memset(cmd, 0, sizeof(cmd));
strcpy(cmd, command);


or do it in 1 line:
Code:
strncpy(cmd, command, sizeof(cmd));
Here, when command is shorter than cmd, the remainder of cmd will be filled with zeroes. There is however one known issue with the strncpy function, that is when the original string is larger than the destination string then the copy will be stopped after n characters leaving a destination string which is not zero terminated. This can be fixed as follows:
Code:
strncpy(cmd, command, sizeof(cmd) - 1);
cmd[sizeof(cmd) - 1]=0;      // ensure string is always zero terminated


If the problem persists after this change, then your problem is number 1) described above.

Note that your original code has another big problem: when command is larger than 100 bytes strcpy will write outside the destination buffer. That's why using strcpy is discouraged, use strncpy instead.


Last edited by ckielstra on Thu Nov 24, 2011 6:09 am; edited 1 time in total
romanrdgz



Joined: 28 Jan 2011
Posts: 16

View user's profile Send private message

PostPosted: Thu Nov 24, 2011 6:08 am     Reply with quote

Sorry, I had tho posts opened and my last answer was for the other one.

I was going to try adding a \0 to the separators, but maybe your solutions are better, so I will give it a try this evening, thanks Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group