String manipulation

Hello
I've a problem with the manipulation of a string. In the variable "response[i].desc" are always strings in the form "Code 123 - blahhblahh".

Now I wanted to copy all characters before the "-" into the variable "pie_legend[z]". The rest should be stripped-off. But... the code below doesn't work :-((

Replacing the line
strncpy (pie_legend[z],response[i].desc,strcspn(response[i].desc,"-"));with
pie_legend[z]=response[i].desc;
works without problems, but then I again have the whole string in it...

Can someone help me?

Thank you very much!
Patrick

u_long pie_data[10];
char *pie_legend[10];
char pie_title[48];
char pie_fname[48];
int z = 0;
/* generate pie chart if needed */

for (i=0;i<TOTAL_RC;i++)
{
if (response[i].count != 0 && z<9)
{
pie_data[z]=response[i].count; /* load the array */
strncpy (pie_legend[z],response[i].desc,strcspn(response[i].desc,"-"));
fprintf(stderr, "%lu / %s\n", pie_data[z], pie_legend[z]);
z+=1;
}
}

sprintf(pie_title,"%s %s %d",msg_ctry_use,l_month[cur_month-1],cur_year);
sprintf(pie_fname,"responses_%04d%02d.png",cur_year,cur_month);

pie_chart(pie_fname,pie_title,t_hit,pie_data,pie_legend); /* do it */
[1465 byte] By [frei] at [2007-11-19 22:05:01]
# 1 Re: String manipulation
strspn()

Kuphryn
kuphryn at 2007-11-9 1:03:03 >
# 2 Re: String manipulation
Sorry, I've been a little bit unprecise... the substring "123" changes, too. I.e. "Code 123 - xyz", "Code 234 - zyz", ...

How can I read out the whole string till the "-" character?? I.e. "Code 123", "Code 234", ...

And... even with a code like:
strncpy (pie_legend[z],response[i].desc,2);I get an error It results in a memory access failure...
frei at 2007-11-9 1:04:03 >
# 3 Re: String manipulation
Is there really no one who can help me??

I already tried everything, but it always gives me a memory access failure. I guess the problem has something to do with char *pie_legend[10];
frei at 2007-11-9 1:04:59 >
# 4 Re: String manipulation
Is this supposed to be C or C++?

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 1:06:05 >
# 5 Re: String manipulation
Try replacing

strncpy (pie_legend[z],response[i].desc,strcspn(response[i].desc,"-"));

with

strncpy (pie_legend[z],response[i].desc,strcspn(response[i].desc,'-'));

because the terminating character is a char, not a string. Maybe this helps?
Ives at 2007-11-9 1:07:09 >
# 6 Re: String manipulation
Try strtok function with '-' as token.
sirikrishnap at 2007-11-9 1:08:08 >
# 7 Re: String manipulation
i tried doin this using my own function... compiles but gives a runtime error - "memory could not be "written"". using vc++6. any suggestions on how to overcome the problem?

#include<iostream>
#include<cstdlib>

using namespace std;

int main()
{
char* ch1=new char[10];
ch1="asdf-;lkj";
char* ch2=new char[10];
ch2="\0";
char temp;
int i=0;
while(1)
{
temp=*ch1++;
if(temp=='-')
break;
*ch2++=temp;
i++;
}
ch2+=-i;
cout<<ch2<<endl;
return 0;
}

regards
ozzy_85 at 2007-11-9 1:09:12 >
# 8 Re: String manipulation
First, thank you for answers.

@ Paul McKenzie: It's C, not C++

@ Ives
With '-' it doesn't work either... --> getting an additional "warning: passing arg 2 of `__builtin_strcspn' makes pointer from integer without a cast."

@ sirikrishnap
That doesn't work --> "pie_legend[z]=strtok (response[i].desc,"-");" results in a memory access failure...

NOTE
The following code works, but results just in the opposite I'd like to have... i.e. it shows me the string after the "-", not the string before it...
pie_legend[z]=strchr(response[i].desc, (int)'-');
frei at 2007-11-9 1:10:13 >
# 9 Re: String manipulation
Replacing the line
strncpy (pie_legend[z],response[i].desc,strcspn(response[i].desc,"-"));with
pie_legend[z]=response[i].desc;
works without problems, but then I again have the whole string in it...

this is dangerous and shuld cause exceptions at end of function.
also strncpy does not copy a nullcharacter at the end of the string, wich causes undefined behavior if not copied.

Can someone help me?

sure... you were on the right track...

size_t len = strchr(response[i].desc, '-') - response[i].desc;
strncpy(pie_legend[z], response[i].desc, len);
pie_legend[z][len] = '\0';
Mitsukai at 2007-11-9 1:11:06 >
# 10 Re: String manipulation
@Mitsukai

That still shows me a memory access failure :-(( Question: When I have defined my variable as...char *pie_legend[10]; doesn't that mean it's a pointer to the variable and it contains a memory address instead of the characters?? So when I want to copy the characters into a memory address an exception will result??

When I set... if (response[i].count != 0 && z<9)
{
pie_data[z]=response[i].count; /* load the array */
fprintf(stderr,"%s / %s\n",pie_legend[z],response[i].desc);
size_t len = strchr(response[i].desc, '-') - response[i].desc;
fprintf(stderr,"%i \n",len);
strncpy(pie_legend[z], response[i].desc, len);
pie_legend[z][len] = '\0';
z+=1;
}
...on the console I get...
% / Code 200 - OK
9
Speicherzugriffsfehler
Note the "%" at the beginning of the first line... there is a strange character in it although there should be nothing in it...
frei at 2007-11-9 1:12:07 >