Link to home
Start Free TrialLog in
Avatar of punktux
punktux

asked on

difficulty in using CString to search string

I am not familiar with CString class, the problem i have is numOfEditBox will not increment if there is more than 1 "editbox" is found.

In generic c++ i can use  while(!read_File.eof()), then read each line. search the second string is easy.

but in MFC, I basically have no idea how to rewrite the while loop in order to find more than 1 "editbox" since i am a very beginner.

DWORD dwRead;
BYTE buffer[20];
 do
      {
               
dwRead = File.Read(buffer, 20);
CString strBuffer(buffer);
if(strBuffer.Find(_T("editBox")))
{      
      numOfEditBox++;
                              
}
      }
      while (dwRead > 0);
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada 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
Avatar of punktux
punktux

ASKER

thanks for helping me twice today ,chensu

xiexie ni le - i assume u can understand this =)
So have you solved your problem?

Bu yong xie.
Avatar of punktux

ASKER

ya sort of give me a hint on how to use cfile::find()

=)

i m actually forced to do a work with inefficient knowledge on mfc ...yun si le.
CFile::Find()? CFile does not have such a member function, does it?
Avatar of punktux

ASKER

sorry i mean cstring::find
haha
// First example demonstrating
// CString::Find ( TCHAR ch )
CString s(_T("abcdef"));
ASSERT( s.Find(_T('c')) == 2 );
ASSERT( s.Find(_T("de")) == 3 );

// Second example demonstrating
// CString::Find( TCHAR ch, int nStart )
CString str(_T("The stars are aligned"));
int n = str.Find(_T('e'), 5);
ASSERT(n == 12);

n = str.Find(_T("star"));
ASSERT(n == 4);

n = str.Find(_T("moon"));
ASSERT(n == -1);
Avatar of punktux

ASKER

thanks chen, but my code just doenst work, the numOfEditBox doesnt increment, i dont know where the error is, i think the problem is  CString strBuffer(buffer);,



int Parser::countEditbox()
{
      CFile xmlFile;
      CFileException e;
      TCHAR* pFileName = _T("test.txt");
      int numofEditBox = 0;

      if( !xmlFile.Open( pFileName, CFile::modeRead | CFile::shareDenyWrite, &e ) )
      {
      #ifdef _DEBUG
      afxDump << "File could not be opened " << e.m_cause << "\n";
      #endif
      AfxMessageBox(_T("Cannot open XML file"));
      }

      else
      {      
                        
            //xmlFile.SeekToBegin();
            TCHAR buffer[100];
            DWORD dwRead;
            dwRead = xmlFile.Read(buffer, 100);
         
      do
      {
         dwRead = xmlFile.Read(buffer, 100);
                  CString strBuffer(buffer);
             int a = strBuffer.Find('e');
              if(a!=-1)
             numofEditBox++;
      }
      while (dwRead > 0);

      // Close both files.

      xmlFile.Close();
}

return numofEditBox;
}

Why don't you set a breakpoint at the line of (strBuffer.Find) and watch the variables to see what is happening?
Avatar of punktux

ASKER

:( my debugger isnt working..yun si le. dont know why

but did i assign the buffer to cstring right?
the cstring seems to contain nothin
Is the file in Unicode? Are you compiling in Unicode?