Click to See Complete Forum and Search --> : How do I find a specified string in a file programmatically


martini
August 6th, 1999, 04:20 AM
Hi,
I'd like to know, given a string how do i find it in a set of files.
For example I have to find a string "CServer" in a set of .cpp files.

How do I implement this in my application?

Thanks in advance

Lothar Haensler
August 6th, 1999, 04:58 AM
you can open each file in your file spec using CStdioFile and scan it using CStdioFile's ReadString method.
Then scan each line using CString's Find method.

P eter C.
August 6th, 1999, 05:03 AM
try that:

with CFile.Read get your file in a CString-buffer and then go ahead as follows...

int Find( LPCTSTR lpszSub ) const;

Return Value
The zero-based index of the first character in this CString object that matches the requested substring or characters; –1 if the substring or character is not found.
Parameters
ch A single character to search for.
lpszSub A substring to search for.
Remarks
Searches this string for the first match of a substring. The function is overloaded to accept both single characters (similar to the run-time function strchr) and strings (similar to strstr).
Example
The following example demonstrates the use of CString::Find.

// example for CString::Find
CString s( "abcdef" );
ASSERT( s.Find( 'c' ) == 2 );
ASSERT( s.Find( "de" ) == 3 );

martini
August 6th, 1999, 05:04 AM
No actually I was looking for a class like CFindFile. Is this the only method I can do it?

Lothar Haensler
August 6th, 1999, 06:27 AM
there is CFileFind class in MFC (not CFindFile).
is this the only method, you ask?
There is never ever only ONE method in programming :-)