[newbie] strcpy, strtok and strcat problem...
- From: pere.noel@xxxxxxxxxxxxxxxxxxx (Une bévue)
- Date: Thu, 31 Aug 2006 19:41:14 +0200
in order not to change an input string i strcpy it to be able to use
strtok and strcat with it, for another reason, i need a second copy of
this string, however, after strtok and strcat with the first, the second
does have exactly the same value, here is the small piece of code for
that part :
--- part of RAliasRecord.c --------------------------------------------
VALUE m_raliasrecord_init(VALUE self, VALUE from_path, VALUE
target_path)
{
char *target_path_cpy, *target_path_cpy2;
target_path_cpy = strcpy(target_path_cpy,
StringValuePtr(target_path));
target_path_cpy2 = strcpy(target_path_cpy2,
StringValuePtr(target_path));
//
// some print-out to verify input state :
//
printf("target_path=%s\n", StringValuePtr(target_path));
// => target_path = /Users/yvon/Desktop/raliasrecord-0.0.1
printf("target_path_cpy=%s\n", target_path_cpy);
// => target_path_cpy = /Users/yvon/Desktop/raliasrecord-0.0.1
printf("target_path_cpy2=%s\n", target_path_cpy2);
// => target_path_cpy2 = /Users/yvon/Desktop/raliasrecord-0.0.1
const char needle[] = "/";
char *token, *file_name;
char parent_path[512]="";
char *pieces[32];
int i = 1;
int j = 1;
token = strtok(target_path_cpy, needle);
strcat(parent_path, needle);
strcat(parent_path, token);
pieces[0]=token;
while(token != NULL) {
pieces[i]=token;
file_name = token;
token = strtok(NULL, needle);
i++;
}
while(j < i-1) {
strcat(parent_path, needle);
strcat(parent_path, pieces[j]);
j++;
}
//
// some print-out to verify output state :
//
printf("target_path=%s\n", StringValuePtr(target_path));
// => target_path = /Users/yvon/Desktop/raliasrecord-0.0.1 GOOD
printf("parent_path=%s\n", parent_path);
// => parent_path = /Users/Users/yvon/Desktop GOOD
printf("file_name=%s\n", file_name);
// => file_name = raliasrecord-0.0.1 GOOD
printf("target_path_cpy=%s\n", target_path_cpy);
// => target_path_cpy = /Users ????
printf("target_path_cpy2=%s\n", target_path_cpy2);
// => target_path_cpy2 = /Users ????
-----------------------------------------------------------------------
i'm not surprised having "target_path_cpy = /Users" because of (strtok
and strcat) however i'm surprised having :
target_path_cpy2 = /Users = target_path_cpy
and, as expected, target_path remains unchanged (thanks to strcpy).
--
une bévue
.
- Follow-Ups:
- Re: [newbie] strcpy, strtok and strcat problem...
- From: Michael Mair
- Re: [newbie] strcpy, strtok and strcat problem...
- From: CBFalconer
- Re: strcpy, strtok and strcat problem...
- From: Dang
- Re: [newbie] strcpy, strtok and strcat problem...
- Prev by Date: Re: Redirection issue
- Next by Date: Re: changing allocated memory
- Previous by thread: how could I write this cpp macro
- Next by thread: Re: strcpy, strtok and strcat problem...
- Index(es):
Relevant Pages
|