Solved

difficulty in using CString to search string

Posted on 2004-03-25
11
489 Views
Last Modified: 2013-11-20
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);
0
Comment
Question by:punktux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 70 total points
ID: 10682107
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
 

Author Comment

by:punktux
ID: 10682219
thanks for helping me twice today ,chensu

xiexie ni le - i assume u can understand this =)
0
 
LVL 23

Expert Comment

by:chensu
ID: 10682360
So have you solved your problem?

Bu yong xie.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:punktux
ID: 10682443
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
 
LVL 23

Expert Comment

by:chensu
ID: 10682697
CFile::Find()? CFile does not have such a member function, does it?
0
 

Author Comment

by:punktux
ID: 10682806
sorry i mean cstring::find
haha
0
 
LVL 23

Expert Comment

by:chensu
ID: 10682882
// 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
 

Author Comment

by:punktux
ID: 10683022
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
 
LVL 23

Expert Comment

by:chensu
ID: 10683070
Why don't you set a breakpoint at the line of (strBuffer.Find) and watch the variables to see what is happening?
0
 

Author Comment

by:punktux
ID: 10683193
:( 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
 
LVL 23

Expert Comment

by:chensu
ID: 10683406
Is the file in Unicode? Are you compiling in Unicode?
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question