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.
Printable View
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.
fgets() + sscanf() - same as always.
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?
show your code and file A + B content with your 'patterns'.
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!
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
}
}