strtok problem...
-
Can anyone see a problem with this code? It keeps resetting the original string line so it jumps out of the loop after the first token. What it should do is parse the whole line... For some reason it doesn't seem to like the "\\" delimiter, if I use " " is works fine - but " " is not the delimiter I need. Thanks for any help in advance ;)
char * token; char dir_name_out[256]; char *delims = "\\"; strcpy(dir_name_out, ""); // Clear output token = strtok(dir_name_in, delims); while(token != NULL) { if(strlen(token) > 8) { token[6] = '~'; token[7] = '1'; token[8] = '\0'; } strcat(token, "\\"); strcat(dir_name_out, token); //strcpy(token, strtok(NULL, "\\\n\0")); token = strtok(NULL, delims); }
u6ik -
Can anyone see a problem with this code? It keeps resetting the original string line so it jumps out of the loop after the first token. What it should do is parse the whole line... For some reason it doesn't seem to like the "\\" delimiter, if I use " " is works fine - but " " is not the delimiter I need. Thanks for any help in advance ;)
char * token; char dir_name_out[256]; char *delims = "\\"; strcpy(dir_name_out, ""); // Clear output token = strtok(dir_name_in, delims); while(token != NULL) { if(strlen(token) > 8) { token[6] = '~'; token[7] = '1'; token[8] = '\0'; } strcat(token, "\\"); strcat(dir_name_out, token); //strcpy(token, strtok(NULL, "\\\n\0")); token = strtok(NULL, delims); }
u6ikIf you are just searching a path for its components, why not use
_splitpath()
or one of the variousPathxxx()
functions from the Shell API? Much better than trying to do it yourself. Also, to create a short filename from a long one, just useGetShortPathName()
.
"Take only what you need and leave the land as you found it." - Native American Proverb
-
If you are just searching a path for its components, why not use
_splitpath()
or one of the variousPathxxx()
functions from the Shell API? Much better than trying to do it yourself. Also, to create a short filename from a long one, just useGetShortPathName()
.
"Take only what you need and leave the land as you found it." - Native American Proverb
-
Cheers Dave. Problem solved. Although.. the original problem is a poser. Still, no time to mess around when you're on a deadline ;) Thanks again. u6ik
u6ik wrote:
Although.. the original problem is a poser.
What is the initial value of
dir_name_in
?
"Take only what you need and leave the land as you found it." - Native American Proverb
-
Can anyone see a problem with this code? It keeps resetting the original string line so it jumps out of the loop after the first token. What it should do is parse the whole line... For some reason it doesn't seem to like the "\\" delimiter, if I use " " is works fine - but " " is not the delimiter I need. Thanks for any help in advance ;)
char * token; char dir_name_out[256]; char *delims = "\\"; strcpy(dir_name_out, ""); // Clear output token = strtok(dir_name_in, delims); while(token != NULL) { if(strlen(token) > 8) { token[6] = '~'; token[7] = '1'; token[8] = '\0'; } strcat(token, "\\"); strcat(dir_name_out, token); //strcpy(token, strtok(NULL, "\\\n\0")); token = strtok(NULL, delims); }
u6ikFirst, here's your code with the indentation restored (note: use the <pre>...</pre> tag when posting more than one line of code):
char * token;
char dir_name_out[256];char *delims = "\\";
strcpy(dir_name_out, ""); // Clear output
token = strtok(dir_name_in, delims);
while(token != NULL)
{
if(strlen(token) > 8)
{
token[6] = '~';
token[7] = '1';
token[8] = '\0';
}strcat(token, "\\");
strcat(dir_name_out, token);
//strcpy(token, strtok(NULL, "\\\n\0"));
token = strtok(NULL, delims);
}A couple of things about
strtok
. One, it modifies the original string, but nothing in your logic has an issue with that. Two, it returns a pointer within the original string. For this reason, operating on thetoken
value is A Bad Idea. The linestrcat(token,"\\");
could overwrite the'\0'
delimiter placed by the preceding call tostrtok
. You would be better off copying the token out to a separate local string, and operating on that.
Software Zen:
delete this;
-
First, here's your code with the indentation restored (note: use the <pre>...</pre> tag when posting more than one line of code):
char * token;
char dir_name_out[256];char *delims = "\\";
strcpy(dir_name_out, ""); // Clear output
token = strtok(dir_name_in, delims);
while(token != NULL)
{
if(strlen(token) > 8)
{
token[6] = '~';
token[7] = '1';
token[8] = '\0';
}strcat(token, "\\");
strcat(dir_name_out, token);
//strcpy(token, strtok(NULL, "\\\n\0"));
token = strtok(NULL, delims);
}A couple of things about
strtok
. One, it modifies the original string, but nothing in your logic has an issue with that. Two, it returns a pointer within the original string. For this reason, operating on thetoken
value is A Bad Idea. The linestrcat(token,"\\");
could overwrite the'\0'
delimiter placed by the preceding call tostrtok
. You would be better off copying the token out to a separate local string, and operating on that.
Software Zen:
delete this;
Thanks Gary. I've made the suggested corrections and all works fine now.
token = strtok(dir_name_in, delims);
strcpy(buf, token);
while(token != NULL)
{
if(strlen(buf) > 8)
{
buf[6] = '~';
buf[7] = '1';
buf[8] = '\0';
}
strcat(buf, "\\");strcat(dir_name_out, buf);
token = strtok(NULL, delims);
}By copying the token into buf and manipulating that, it doesn't effect the operation of the strtok function. Thanks again :) u6ik