Link to home
Start Free TrialLog in
Avatar of applebibo
applebibo

asked on

How to convert CString to int?

I cannot figure out why the following codes won't work to change a string to an interger. Please help!! The numbers are stored in the file seperated by single space. And the error code is: " error C2664: 'atoi' : cannot convert parameter 1 from 'CString' to 'const char *' "

void CGrindGameDlg::OnBnClickedFileOpen()
{
      // TODO: Add your control notification handler code here
      CStdioFile fBoundary;
      CString temp;

      CFileDialog FileDlg(TRUE, _T(".txt"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Points files (*.txt)|*.txt|All Files (*.*)|*.*|" ), NULL, 0);
      
      if( FileDlg.DoModal() == IDOK )
      {
            if( fBoundary.Open(FileDlg.GetFileName(), CFile::modeRead) == FALSE )
                  return;
            
            temp=FileDlg.GetFileName();

            // read the boundary points from the file and saved in vector sbound
            vector <i32> sboundx, sboundy;
            CString point;
            int n1,n2;
            int t;
            while (fBoundary.ReadString(point))
            {
                  n1=point.Find(' ');
                  temp=point.Left(n1+1);
                  t=atoi(temp);
                  sboundx.push_back(t);

                  n2=point.Find(' ',n1);
                  temp=point.Mid(n1+1,n2+1);
                  t=atoi(temp);
                  sboundy.push_back(t);
            }
}
Avatar of jkr
jkr
Flag of Germany image

Just use

 t=atoi((LPCTSTR)temp);

to get a 'char*' from the CString, then it will work.
Avatar of applebibo
applebibo

ASKER

It did not work:
error C2664: 'atoi' : cannot convert parameter 1 from 'LPCTSTR' to 'const char *'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Thanks!
Are you changing that at both locations?

            while (fBoundary.ReadString(point))
            {
                  n1=point.Find(' ');
                  temp=point.Left(n1+1);
                  t=atoi((LPCTSTR)temp);
                  sboundx.push_back(t);

                  n2=point.Find(' ',n1);
                  temp=point.Mid(n1+1,n2+1);
                  t=atoi((LPCTSTR)temp);
                  sboundy.push_back(t);
            }

If that still does not work, try

            while (fBoundary.ReadString(point))
            {
                  n1=point.Find(' ');
                  temp=point.Left(n1+1);
                  t=atoi(temp.GetBuffer(0));
                  temp.ReleaseBuffer();
                  sboundx.push_back(t);

                  n2=point.Find(' ',n1);
                  temp=point.Mid(n1+1,n2+1);
                  t=atoi(temp.GetBuffer(0));
                  temp.ReleaseBuffer();
                  sboundy.push_back(t);
            }
Thanks! Yes, I did change both locations. And I just tried your code, but there are still errors:

error C2664: 'atoi' : cannot convert parameter 1 from 'wchar_t *' to 'const char *'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

What's wrong with my Visual C++ 2005?
>>error C2664: 'atoi' : cannot convert parameter 1 from 'wchar_t *'
>>to 'const char *'

Ah, you are using UNICODE - then you'll need to use '_wtoi()' instead
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for getting this headache out of me!!!! You ARE genius!!!!
Then do I need to change the function name when I compile it on other computers that are not using UNICODE?
If you use '_ttoi()', you won't have to change anything.
I get it. I can set it in the project properties.