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);
punktuxAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
chensuConnect With a Mentor Commented:
CString::Find() returns the zero-based index of the first character that matches the requested substring or characters; -1 if the substring or character is not found. So your if condition is incorrect.
0
 
punktuxAuthor Commented:
thanks for helping me twice today ,chensu

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

Bu yong xie.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
punktuxAuthor Commented:
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.
0
 
chensuCommented:
CFile::Find()? CFile does not have such a member function, does it?
0
 
punktuxAuthor Commented:
sorry i mean cstring::find
haha
0
 
chensuCommented:
// 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);
0
 
punktuxAuthor Commented:
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;
}

0
 
chensuCommented:
Why don't you set a breakpoint at the line of (strBuffer.Find) and watch the variables to see what is happening?
0
 
punktuxAuthor Commented:
:( 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
0
 
chensuCommented:
Is the file in Unicode? Are you compiling in Unicode?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.