Thread: How do I scan patterns?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    32

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    fgets() + sscanf() - same as always.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    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?

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    show your code and file A + B content with your 'patterns'.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    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

    Please help!
    Last edited by unknownC; 10-16-2011 at 02:49 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    That's why you read a line using fgets(), then use sscanf() (or whatever) when the line is in memory.

    fscanf() works well when the data is a known format, but if you have a variable format, it doesn't work so well.

    Code:
    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
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design patterns in C
    By sashaKap in forum C Programming
    Replies: 2
    Last Post: 04-26-2009, 08:32 AM
  2. c programming on patterns
    By himankinishah in forum C Programming
    Replies: 2
    Last Post: 01-27-2009, 03:54 PM
  3. Printing Patterns
    By ferniture in forum C Programming
    Replies: 11
    Last Post: 11-17-2008, 10:00 PM
  4. Creating Patterns
    By incognito in forum Game Programming
    Replies: 5
    Last Post: 03-16-2003, 09:02 AM
  5. Patterns
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-29-2002, 04:02 PM