RE: Question about different string type in VC++
- From: Geo <Geo@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 7 Oct 2005 11:34:02 -0700
Hi,
Since you have probably modified the string, you have to call
ReleaseBuffer(). Your
void CSimpleDlg::OnBTNTest()
{
// TODO: Add your control notification handler code here
CString sInput;
LPSTR sTemp;
GetDlgItemText(IDC_TXTInput,sInput); // get the string to be
trimmed
sTemp=Trim(sInput.GetBuffer(0));
sInput.ReleaseBuffer(); // must be called since 'sInput' might have
changed
GetDlgItem(IDC_TXTOutput)->SetWindowText(sTemp); // display output
}
Hope this helps,
Geo
"xg" wrote:
> Hi, I want to write a simple Trim() function to trim the spaces around a
> LPSTR string. I created a dialog based app to test the routine, the Trim()
> function seems to be OK, but when I close the dialog, it causes General
> Protection Fault. I tried some other approaches but no go. Since I only
> intent to use Ansi string, so I did not use any _T() macro or _TCHAR() macro
> in Trim(). Please tell me what I did wrong. Thanks.
>
> Here are the code fragments:
>
> void CSimpleDlg::OnBTNTest()
> {
> // TODO: Add your control notification handler code here
> CString sInput;
> LPSTR sTemp;
>
> GetDlgItemText(IDC_TXTInput,sInput); // get the string to be
> trimmed
>
> sTemp=Trim(sInput.GetBuffer(0));
>
> GetDlgItem(IDC_TXTOutput)->SetWindowText(sTemp); // display output
> } // after the function
> exits, Crash!
>
> LPSTR Trim(LPCSTR s)
> {
> LPSTR sResult=new (CHAR);
> int i;
>
> if(s==NULL) return NULL;
>
> lstrcpy(sResult,s);
>
> for(i=0;i<lstrlen(sResult)&&sResult[i]==' ';i++);
> sResult+=i;
>
> for(i=lstrlen(sResult)-1;i>0&&sResult[i]==' ';i--);
> *(sResult+i+1)='\0';
>
> return sResult;
> }
>
>
>
.
- Prev by Date: Re: Complex MFC State Issue.
- Next by Date: Re: Question about threads and Events
- Previous by thread: CreateFile and directories/files with spaces in them
- Next by thread: Re: Question about different string type in VC++
- Index(es):
Relevant Pages
|