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

    Function to find special String

    I need a function that will search a file byte by byte until it reaches a special string "<!--$$(" when this is found it will extract what it is found until it reaches the ")". There will be more than just a once occurence of that special string.

    Does anyone have an exampple of something similar or could someone point me in the right direction. I was hoping to use something like Mid from VB but it doesn't seem to have the same function in C++.

    Any help would be greatly appreciated


  2. #2
    Join Date
    May 1999
    Location
    Antwerp, Belgium
    Posts
    136

    Re: Function to find special String

    One possible algorithme is this :

    Read a char from the file. If the char is equal to the first char of the searchstring then add one to a counter. Read the next char
    and check it with the second(= counter) char of your searchstring. If the char is not equal reset counter to 0. If counter is
    equal to the length of your searchstring then you have found the string in the file.

    Note you can use following functions like the mid in VB :

    for char* : strstr()
    CString-classes :
    Mid()-method
    Find()-method
    std::string-class :
    substr()-method
    find()-method


  3. #3
    Join Date
    Apr 1999
    Posts
    121

    Re: Function to find special String

    Would you have an example I could look at...I'm pretty new at this C++

    Thank you


  4. #4
    Guest

    Re: Function to find special String

    CString some_test_str = "this is my target !<some_thing_to_find>";

    int start_pos = some_test_str.Find("!<");
    int stop_pos = some_test_str.Find(">");
    ASSERT(start_pos != -1 && stop_pos !=-1);
    ASSERT(start_pos + 2 != stop_pos); // check that something is contained by !<>
    CString found_str = some_test_str.Mid(start_pos + 2,stop_pos - start_post - 2);


  5. #5
    Join Date
    Jun 1999
    Location
    Lumberton, NJ
    Posts
    70

    Re: Function to find special String

    I did exactly this in an application I'm working on: (I read the file in pages)

    #define BUFSIZE 8192
    CString searchText( "search for this" );
    FILE *fp = fopen( "file.dat", "wt" );
    char *buffer = new char[ BUFSIZE + 1 ];
    fseek( fp, SEEK_SET, 0 );
    while( !feof( fp ) )
    {
    long slen = searchText.GetLength();
    unsigned long curr = ftell( fp );
    memset( buffer, '\0', BUFSIZE + 1 );
    if( curr > (unsigned)slen )
    fseek( fp, SEEK_CUR, -(slen) );
    int len = fread( buffer, BUFSIZE, 1, fp );
    if( len )
    {
    char *p = buffer;
    while( p = strstr( p, searchText ) )
    {
    // p now points to an occurrence of
    // search string
    ...
    p += searchText.GetLength();
    }
    }
    }
    delete [] buffer;
    fclose( fp );





    Hope this helps.


  6. #6
    Join Date
    Jun 1999
    Location
    Lumberton, NJ
    Posts
    70

    Re: Function to find special String

    ummm... I meant to fopen with "rt", not "wt"...

    Sorry.


  7. #7
    Join Date
    May 1999
    Location
    Antwerp, Belgium
    Posts
    136

    Re: Function to find special String

    This is an example :


    #include "stdio.h"
    #include "string.h"

    void main(void)
    {
    char *s = "<!--$$(";

    FILE *f = fopen("test.txt", "r");
    if ( f )
    {
    int nCount = 0;
    while ( ! feof(f) )
    {
    if ( fgetc(f) == s[nCount] )
    {
    if ( s[++nCount] == '\0' )
    {
    printf("string found start at : %ld\n", ftell(f) - nCount);
    nCount = 0;
    }
    }
    else
    nCount = 0;
    }
    fclose(f);
    }
    }





  8. #8
    Join Date
    May 1999
    Location
    Antwerp, Belgium
    Posts
    136

    Re: Function to find special String

    There is an error in your code :

    fseek( fp, SEEK_CUR, -(slen) );



    should be

    fseek( fp, -slen + 1, SEEK_CUR);



    And another warning :
    Always test if the fopen-function succeeded !!! Reading from a NULL-pointer will cause severe errors.


  9. #9
    Join Date
    Jun 1999
    Location
    Lumberton, NJ
    Posts
    70

    Re: Function to find special String

    Excellent catch!
    But, I was re-typing this from my laptop's display into my desktop's browser and it was a typo... It's right in the original! And, I took this code from a method where the FILE * is a member var, and yes I always check it for NULL. This was just a simple example of how to do the search, and not intended to be a perfect implementation. ;->


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