Re: strcpy, strtok and strcat problem...
- From: "Dang" <dangamit@xxxxxxxxx>
- Date: 31 Aug 2006 10:54:07 -0700
Une bévue wrote:
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
Hi, if this is the actual code then I can see some issue. In function
"m_raliasrecord_init" you have taken two char pointers target_path_cpy,
target_path_cpy2 and you are directly doing a strcpy. Who is allocation
memory for them?
Regards,
Dang
.
- Follow-Ups:
- Re: strcpy, strtok and strcat problem...
- From: Une bévue
- Re: strcpy, strtok and strcat problem...
- References:
- [newbie] strcpy, strtok and strcat problem...
- From: Une bévue
- [newbie] strcpy, strtok and strcat problem...
- Prev by Date: Re: changing allocated memory
- Next by Date: Re: A newbie's code
- Previous by thread: [newbie] strcpy, strtok and strcat problem...
- Next by thread: Re: strcpy, strtok and strcat problem...
- Index(es):
Relevant Pages
|