CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 1999
    Posts
    68

    How do I find a specified string in a file programmatically

    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



  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: How do I find a specified string in a file programmatically

    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.


  3. #3
    Join Date
    Jun 1999
    Posts
    10

    Re: How do I find a specified string in a file programmatically

    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 );






  4. #4
    Join Date
    May 1999
    Posts
    68

    Re: How do I find a specified string in a file programmatically

    No actually I was looking for a class like CFindFile. Is this the only method I can do it?


  5. #5
    Join Date
    May 1999
    Posts
    3,332

    Re: How do I find a specified string in a file programmatically

    there is CFileFind class in MFC (not CFindFile).
    is this the only method, you ask?
    There is never ever only ONE method in programming :-)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured