Solved

difficulty in using CString to search string

Posted on 2004-03-25
11
483 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Installing Microsoft R server on REDHAT or SUSE Linux 4 125
Add content to output file 4 57
Hibernate methods 2 58
canBalance challenge 34 65
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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 …
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now