# How do I scan patterns?

• 10-16-2011
unknownC
How do I scan patterns?
I need to scan patterns (file A) on a matrix file B.

file A looks something like this...
1 1
1 1
*******
0 0
0 0
*******
2 2
2 2

Please give me some clues how it can be done.
• 10-16-2011
Salem
fgets() + sscanf() - same as always.
• 10-16-2011
unknownC
Tell me if I am correct/wrong:

scanf for file A till it hits '*'
for loop for file B and scanf

But how do I identify that as 1 pattern? then how do I get between "*" and "*" is another pattern?
• 10-16-2011
BillyTKid
show your code and file A + B content with your 'patterns'.
• 10-16-2011
unknownC
Code:

```#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #define size 25 main() { time_t start, Totalstart; time_t finish, Totalfinish; int matrix[size][size]; int patterntxt[size][size]; int parent[size][size]; int i,j; int m,n; int Xcoordinate; int Ycoordinate; int pattern=0; int num=1; int a; FILE *MatrixA; FILE *Patterns; MatrixA = fopen("MatrixA.txt","w"); /*It's a rand() generated. Not going to show here.*/ fclose(MatrixA); /*Scanning the patterns*/ ParentMatrix = fopen("MatrixA.txt","r"); Patterns = fopen("Patterns.txt","r"); /*Pattern starts*/ do { printf("Pattern can be found at "); for(m=0; m!='='; m++)/*Get pattern from Patterns.txt*/ {     for(n=0; n!='\0'; n++)     { for(i=0; i<size; i++)/*Get from MatrixA.txt*/     {         for(j=0; j<size; j++)         {             fscanf(ParentMatrix, "%d ", &parent[i][j]);             fscanf(Patterns, "%d ", &patterntxt[m][n]);             time(&start);             if (parent[i][j]==patterntxt[m][n] && parent[i][j+1]==patterntxt[m][n+1] && parent[i][j+2]==patterntxt[m][n+2] && parent[i+1][j]==patterntxt[m+1][n] && parent[i+1][j+1]==patterntxt[m+1][n+1] && parent[i+1][j+2]==patterntxt[m+1][n+2] && parent[i+2][j]==patterntxt[m+2][n] && parent[i+2][j+1]==patterntxt[m+2][n+1] && parent[i+2][j+2]==patterntxt[m+2][n+2])             {                 /*printf something*/             }             else             {                 pattern=0;             }             time(&finish);         }     }             printf("\nNumber of patterns found: %d\n", pattern);             printf("Time Taken: %.6f seconds\n", difftime(finish,start));     } } }while(patterntxt[m][n]!='='); /*end of Pattern*/  printf("Search Done!\n");  fclose(MatrixA); fclose(Patterns);  }```

FileA...

1 1
1 1
*****
0 0
0 0
*****
2 2
2 2

```char buff[BUFSIZ]; while ( fgets( buff, BUFSIZ, fp ) != NULL ) {   if ( buff[0] == '*' ) {     // line beginning with * - do something   } else   if ( sscanf( buff, "%d %d", &x, &y ) == 2 ) {     // line containing 2 ints - do something   } else {     // some other crap   } }```