By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

pattern matching program problem

P: 1
hi friends this is arun from coimbatore.i had written one program for pattern matching.That is ,the user have one file and after run his c program he pass some text command line, if the text is found in file the line number will be print.but my program checks only first two lines.it will not check other lines.i am trying to find out the prob.plz anybody help me to find wat wrong in my program.
thanking u.
the program is:

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. main(int x,char *j[])
  5. {
  6. int g,h,z=1;
  7. char ch,c[30]=" ",d[30]=" ",a[30]=" ";
  8. int n,i=0,line=0;
  9. FILE *f;
  10. printf("*****************FILE NAME IS:%s\n",j[1]);
  11. f=fopen(j[1],"r");
  12. if(f!=NULL)
  13. printf("*****************FILE OPENED****************\n");
  14. for(n=2;n<x;n++)
  15. {
  16. strcat(c,j[n]);
  17. if(n!=x-1)
  18. strcat(c," ");
  19. }
  20. strcpy(a,c);
  21. for(g=0,h=1;a[g]!='\0';g++,h++)
  22. a[g]=a[h];
  23. a[g]='\0';
  24. printf("******************SEARCH TEXT IS:%s\n",a);
  25. while((z=feof(f))==0)
  26. {
  27. fseek(f,0,1);
  28. while((ch=fgetc(f))!='\n')
  29. i++;
  30. i=i+2;
  31. line++;
  32. fseek(f,-i,1);
  33. fgets(d,i,f);
  34. if((strstr(d,a))!=NULL)
  35. {
  36. printf("******************LINE NO IS=>%d  :%s\n",line,d);
  37. }
  38. else
  39. {
  40. printf("*****************LINE NO IS=>%d\t***********NOT FOUND\n",line);
  41. }
  42. i=0;
  43. }
  44. }
Jan 19 '08 #1
Share this Question
Share on Google+
1 Reply


Ganon11
Expert 2.5K+
P: 3,652
I'm sorry that I can't give you an answer to your immediate question, but I suggest you take the time to learn proper indenting. As it is right now, your code is a horrible mess that's hard to decipher. Taking 5 minutes or so to go through it and indent it properly - two or three spaces per block a statement is embedded in - will make your life MUCH easier when you go back to edit it. Better yet, do it while you are coding.
Jan 19 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.