Hi All, I wanna to search a pattern in a txt file called A which is a 2D matrix from another file called B. The requirment is that I need to output the number of matched pattern in file B. But I encounted difficulty of counting the number.

Basically I am trying to input to matrix from A to a tmp_matrix, and then I am using for loop to search the pattern char by char.

I am too new to know that if there is other existing function can do such job. Can you suggest a bit too me, or help to look at the code below. Thank you so much.

#include<stdio.h>		//Header file
#include<stdlib.h>
#include<time.h>
#include<iostream>
#include<math.h>

using namespace std;

#define MAXROWS 100		//Define max rows =10		
#define MAXCOLS 100		//Define max cols =10

void readinput(int in[][MAXCOLS]);		//Function prototype	
//void writeoutputA(int out[][MAXCOLS]);	
//void writeoutputB(int count);

void main()
{
		int random_matrix[MAXROWS][MAXCOLS];
		int tmp_matrix[MAXROWS][MAXCOLS];
		int i,j;
		int count = 0;

		{
			FILE *inputFile=fopen("Patterns.txt","r");	
			for(int row=0; row<2; row++)	/* to keep on reading every number */
			{   
				for(int col=0; col<2; col++)			
				{
					fscanf(inputFile,"%d",&tmp_matrix[row][col]);
				}
			}
			fclose(inputFile); /*Close the file stream*/
		}

		srand((unsigned)(time(NULL)));	/* initialize random seed: */
		  for (i=0;i<MAXROWS;i++)			  /* generate random number: */
		  {
			  for (j=0;j<MAXCOLS;j++)
			  {
			  random_matrix[i][j]= rand() % 2;
			  }
		  }
		  
//		  {
//			  writeoutputA(random_matrix);		  
//		  }			
		  printf ("Random matrix generated successfully, presee [ENTER] to continues...");
		  getchar();

		  int result;

		  for (i=0;i<MAXROWS;i++)
		  {
			  for (j=0;j<MAXCOLS;j++)
				  {
					  for (int x=0; x<3; x++)
					  {						  
						  for (int y=0; y<3; y++)
						  {
							  if (tmp_matrix[x][y] == random_matrix[i+x][j+y])
								  continue;
							  else
							  {
								  break;
							  }
						  }
					  }
					   //writeoutputB(count);
					}
		  }			
		  printf("number of pattern found is %d\n\n",count);

		  system("pause"); 

}

Recommended Answers

All 3 Replies

This is a common job that can be done in different ways, but the easiest one is probably using fgets() to read every line of the file (this a text file, right?), and then searching for the pattern you want, within that buffer.

It would start like this:

#define MAX 100

char buff[MAX];
char *pPattern=NULL;

open the file here


while((fgets(buff, MAX, yourFilePointer))!=NULL) {
   pPattern = strstr(buff, "YourPattern");
   if(pPattern) {
      //your code to record that you found the pattern
   }else {
      //pattern was not found in this line of text
   }
}

If you pattern could reach across more than one row of text, then using a larger buffer with fgetc(), may be needed.

Your headers all indicate a C compiler, but namespace indicates you may be using a C++ compiler, instead. I'd advise using either the C way, or the C++ way of doing this, but not a mixture of both. Check your project options or compiler options, to see which one you're actually using.

Thanks for your help, I am not very sure which namespace is using c++ complier, can you help to highlight for me. as I now running with c, it has the cmath error...

This is a common job that can be done in different ways, but the easiest one is probably using fgets() to read every line of the file (this a text file, right?), and then searching for the pattern you want, within that buffer.

It would start like this:

#define MAX 100

char buff[MAX];
char *pPattern=NULL;

open the file here


while((fgets(buff, MAX, yourFilePointer))!=NULL) {
   pPattern = strstr(buff, "YourPattern");
   if(pPattern) {
      //your code to record that you found the pattern
   }else {
      //pattern was not found in this line of text
   }
}

If you pattern could reach across more than one row of text, then using a larger buffer with fgetc(), may be needed.

Your headers all indicate a C compiler, but namespace indicates you may be using a C++ compiler, instead. I'd advise using either the C way, or the C++ way of doing this, but not a mixture of both. Check your project options or compiler options, to see which one you're actually using.

I can't help you with C++. I don't know what "the cmath error" is. C has no "namespace", and if you need help in C++, you should go to that forum, and repost your questions there.

Check your project options or compiler options. Also, your programs source code should have a .c file name extension for a C file. A C++ file extension would be .cpp.

In either language, void main() should be changed to int main() or int main(void), and return 0; should be added to the last line of main.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.