|
-
June 17th, 2006, 08:55 AM
#1
[RESOLVED] problems with strncpy
Hi for all!
I want to cut last word in the string and then reflect it on the CStatic-component.
The function "cutting_space", as far as I understand must do cutting of spaces :
Code:
char *cutting_space(const char *source)
{
int i,k;
char *ds;
for(i=strlen(source)-1;i>=0;i--)
if(source[i]==' ')
{
ds=new char[i];
strncpy(ds,source,i);
if(i>=1 && source[i-1]!=' ')
{
k=10;
break;
}
};
if (k!=10)
{
ds=new char[strlen(source)];
strncpy(ds,source,strlen(source));
}
return(ds);
};
I put result of "cutting_space" into the CStatic with the following code:
Code:
CRect rcwin(0,0,200,70), rcstat;
rcstat.left=0;
rcstat.right=rcwin.right-rcwin.left;
rcstat.top=0;
rcstat.bottom=40;
MyStatic = new CStatic();
char *r="ASDASDASDASDASDASDASDASASDASDASD sdfsdf sdf sdf sfa a ret eirh oiughofdihodiuoifudhgoidfghofgsdfghsoihgphwq[yqpiperuhpfdfsfwerwerwerwerwerwerrrrrrrrrrrrrrrrrrrrrrrrr";
char *we=cutting_space(r);
if (MyStatic!=NULL) MyStatic->Create("XXX",WS_CHILD|WS_VISIBLE|SS_CENTER|SS_CENTERIMAGE,rcstat,this);
CSize ts=MyStatic->GetDC()->GetTextExtent(we);
rcwin.right=ts.cx+4*GetSystemMetrics(SM_CXFRAME);
MoveWindow(rcwin,WM_PAINT);
int rcwidth=rcwin.right-rcwin.left, rcheight=rcwin.bottom-rcwin.top;
MyStatic->MoveWindow(0,0,rcwidth-2*GetSystemMetrics(SM_CXFIXEDFRAME),40,WM_PAINT);
MyStatic->CenterWindow(this);
this->CenterWindow(NULL);
MyStatic->SetWindowText(we);
this->SetWindowText("as");
And I have a kind of 
Help, please...
-
June 17th, 2006, 02:27 PM
#2
Re: problems with strncpy
What is that cutting_space function suppose to do? I'm sure that can be written in a more safe way instead of all those strcpy's and new allocations.
Also, the image you've posted doesn't show up.
-
June 18th, 2006, 01:31 AM
#3
Re: problems with strncpy
You're chopping off the '\0' end string character I believe (unless strncpy adds that for you...not sure). I think you have to append it yourself.
Also, I guess we just have to assume you're deleting both those char arrays somewhere else....
-
June 18th, 2006, 07:05 AM
#4
Re: problems with strncpy
 Originally Posted by DarkCount
Hi for all!
I want to cut last word in the string and then reflect it on the CStatic-component.
The function "cutting_space", as far as I understand must do cutting of spaces  :
You are using C++, so why are you doing this in such a convoluted way using strcpy(), new, and char pointers/arrays? I can almost bet the program has memory leaks, since you allocate in this "cutting_spaces" function, but nowhere do you delete this memory. And why does "k" take on the magic value of 10? What is the significance of k being equal to 10?
Why not just copy the passed in string to a CString and use the CString functions to do this work? It is much safer and easier than the code you're showing, and you don't need to allocate anything to do the work.
Regarcds,
Paul McKenzie
Last edited by Paul McKenzie; June 18th, 2006 at 07:08 AM.
-
June 18th, 2006, 07:17 AM
#5
Re: problems with strncpy
Using CString:
Code:
CString cutting_space(const CString& source)
{
int spacepos = source.ReverseFind(' ');
if (spacepos == -1)
return source;
if ( spacepos == source.Length()-1)
return "";
return source.Mid(spacepos+1);
}
Regards,
Paul McKenzie
-
June 19th, 2006, 10:10 AM
#6
Re: problems with strncpy
 Originally Posted by Marc G
Also, the image you've posted doesn't show up.
The image shows up To see this "picture", it's necessary to copy address of the image to Address bar of your browser. At least I see it only this way... 
I'm sorry for inconveniences...
2 Paul McKenzie: Thanks a lot for your example, it was very useful for me!
-
June 19th, 2006, 10:54 AM
#7
Re: problems with strncpy
Dark, because that code you posted is of such high standards, you have been awarded today's special price
-
June 19th, 2006, 02:52 PM
#8
Re: problems with strncpy
Ok, zerver. Especially for you:
By the way, I think this smile can deverse for add to collection on the "CodeGuru" forum... I wasn't mistaken?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|