[newbie] strcpy, strtok and strcat problem...



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
.



Relevant Pages

  • Re: [newbie] strcpy, strtok and strcat problem...
    ... 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 ...
    (comp.lang.c)
  • Re: [newbie] strcpy, strtok and strcat problem...
    ... 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 ...
    (comp.lang.c)
  • Re: Undeclared identifier error in file "xlocnum"
    ... I had issues with strcat and strcpy in 1975, and nothing in the intervening 30 years has ... you have done something that includes the safe-string library; ... you should not be using sprintf any longer. ...
    (microsoft.public.vc.mfc)
  • Re: Undeclared identifier error in file "xlocnum"
    ... there is still a reason to use those crappy old functions. ... >your code is used on other platforms and by other compilers you will ... >>strcpy or strcat these days, it is better to adopt safe programming practices. ...
    (microsoft.public.vc.mfc)
  • Re: Difference between strcpy() and strcat()?
    ... Leor Zolman wrote in message news:> "strcpy is safer than strcat because it is easier to check programmatically ... > length of the source string. ... With strcat, on the other hand, you have to ...
    (comp.lang.c)