On 23/04/2018 21:45, Stefan Ram wrote:
MRAB <pyt...@mrabarnett.plus.com> writes:
offset += search_length

   Or, offset += 1, to also find overlaps of that kind:

file = "eee"
word = "ee"

   . The above word "ee" occurs at position 0 and 1 in the file.

   My attempt:

#include <stdio.h>
#include <string.h>
int main( void )
{ FILE * const haystack = fopen( "filename.dmp", "r" );
   if( !haystack )goto end;
   char const * const needle = "bd:mongo:";
   int offset = 0;
   int const l =( int )strlen( needle );
   { int o[ l ]; /* VLA */
     for( int i=0; i < l; ++i )o[ i ]= -1;
     o[ 0 ]= 0;
     next: ;
     int const x = fgetc( haystack );
     if( x == EOF )goto out;
     ++offset;
     for( int i=0; i < l; ++ i )
     if( o[ i ]>= 0 )
     { char const ch = needle[ o[ i ] ];
       if( ch == x )++o[ i ];
       if( o[ i ]==( int )strlen( needle ))
       { printf( "found at %d\n", offset -( int )strlen( needle )); }}
     for( int i = l; i; --i )o[ i ]= o[ i - 1 ];
     o[ 0 ]= 0;
     goto next;
     out: fclose( haystack ); }
   end: ; }


Did you say that you teach programming?

--
bartc
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to