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
Solved

difficulty in using CString to search string

Posted on 2004-03-25
11
486 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to monitor remote shell execution on linux 9 104
java ^ examples 8 69
Problem to event 3 102
JQuery serialize and unserialize 8 135
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.

839 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