CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2006
    Posts
    67

    [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...

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    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.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Nov 2005
    Posts
    63

    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....

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: problems with strncpy

    Quote 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.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    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

  6. #6
    Join Date
    May 2006
    Posts
    67

    Re: problems with strncpy

    Quote 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!

  7. #7
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: problems with strncpy

    Dark, because that code you posted is of such high standards, you have been awarded today's special price

  8. #8
    Join Date
    May 2006
    Posts
    67

    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
  •  





Click Here to Expand Forum to Full Width

Featured