find string in file

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 459
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 38
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training

find string in file

 
0
  #1
33 Days Ago
Hello friends, I am having a problem writing a function to search for a string in an input file. I have successfully output the file with line numbers, which was the first step in the program, but now I am stuck on searching for a given string. After searching for the string I want to print the number of times it appears in the input file. As of right now all the function does is return a junk number. I have tried many different things and haven't gotten anything to work correctly.
Here is my code
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX 1000
  4. void find_string(char text[], char pattern[]);
  5. int main()
  6. {
  7. char pattern[] = "of"; //string to search for
  8. int counter = 0;
  9. static const char filename[] = "in.txt";
  10. FILE *file = fopen(filename, "r");
  11. if ( file != NULL )
  12. {
  13. char line [ MAX ];
  14.  
  15. while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
  16. {
  17. ++counter;
  18. printf( "%d ", counter);
  19. fputs ( line, stdout ); /* write the line */
  20. }
  21. printf( "\n\n");
  22.  
  23. find_string(filename, pattern);
  24. fclose ( file );
  25. }
  26. else
  27. {
  28. perror ( filename );
  29. }
  30.  
  31. char wait;
  32. scanf( "%c", &wait );
  33. return(0);
  34. }
  35.  
  36. void find_string(char text[], char pattern[])
  37. {
  38. int matches;
  39. static const char filename[] = "in.txt";
  40. FILE *file = fopen(filename, "r");
  41. if ( file != NULL )
  42. {
  43. char line [ MAX ];
  44.  
  45. while ( fgets ( line, sizeof line, file ) != NULL )
  46. {
  47. if( line == pattern)
  48. matches++;
  49. }
  50. printf("%d", matches);
  51. }
  52. }
Thanks to anyone who takes the time to point me in the right direction.

I attached the input file as an attachment.
Thanks again for any help.
Last edited by Grn Xtrm; 32 Days Ago at 6:13 pm.
Attached Files
File Type: txt in.txt (526 Bytes, 6 views)
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
1
  #2
32 Days Ago
  1. if( line == pattern)
That's a no-no, since line and pattern are strings and you can't compare arrays in that way.
Take a look at the string function strcmp() for that.
However, even if that would be correct, line is bound to have more characters read into, that the pattern, (unless fgets() by coincidence reads only the pattern).
Maybe the function strstr() would be more helpful.

You open the file in main() and read from it, then you open the same file in the function and read from it, you do not close it in the function, then close it in main. I'll say there's something odd, don't you think?
Last edited by Aia; 32 Days Ago at 6:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 459
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 38
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #3
32 Days Ago
Yes Aia you are right that there are a few dumb mistakes in my code.
I did some searching and found the following code using strstr.
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. #define SIZE 100
  6.  
  7. void search_string(char *);
  8.  
  9. int main(void)
  10. {
  11. char array[SIZE];
  12.  
  13. puts("Enter a string:");
  14. gets(array);
  15. search_string(array);
  16.  
  17. char wait;
  18. scanf( "%c", &wait );
  19. return 0;
  20. }
  21.  
  22. void search_string(char *array)
  23. {
  24. char *ptr, *strptr, word[SIZE] = {""};
  25. int i = 0, j = 0;
  26.  
  27. ptr = array;
  28.  
  29. while (*ptr != '\0')
  30. {
  31. if (isspace(*ptr))
  32. break;
  33. word[i] = array[i];
  34. ptr++;
  35. i++;
  36. }
  37. strptr = array;
  38.  
  39. while ((strptr =strstr(strptr, word)) != NULL)
  40. {
  41. strptr++;
  42. j++;
  43. }
  44.  
  45. puts(array);
  46. printf("%s occured %d times in the string\n", word, j);
  47. }
I have tried to apply this code to my porblem but have been unsuccessful. Here is what I tried.
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define MAX 1000
  5. #define SIZE 100
  6.  
  7. void search_string(char *file);
  8.  
  9. int main()
  10. {
  11. char pattern[] = "of";
  12. int counter = 0;
  13. const char filename[] = "in.txt";
  14. FILE *file = fopen(filename, "r");
  15. if ( file != NULL )
  16. {
  17. char line [ MAX ];
  18.  
  19. while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
  20. {
  21. ++counter;
  22. printf( "%d ", counter);
  23. fputs ( line, stdout ); /* write the line */
  24. }
  25. printf( "\n\n");
  26.  
  27. search_string(file);
  28. fclose ( file );
  29. }
  30. else
  31. {
  32. perror ( filename );
  33. }
  34.  
  35. char wait;
  36. scanf( "%c", &wait );
  37. return(0);
  38. }
  39.  
  40.  
  41. void search_string(char *file)
  42. {
  43. char *ptr, *strptr, word[SIZE] = {"of"};
  44. int i = 0, j = 0;
  45.  
  46. ptr = file;
  47.  
  48. while (*ptr != '\0')
  49. {
  50. if (isspace(*ptr))
  51. break;
  52. word[i] = file[i];
  53. ptr++;
  54. i++;
  55. }
  56. strptr = file;
  57.  
  58. while ((strptr = strstr(strptr, word)) != NULL)
  59. {
  60. strptr++;
  61. j++;
  62. }
  63. printf("%s occured %d times in the string\n", word, j);
  64. }
However, when I run the code it outputs 0 as the number of times the word occurs.
I feel that by changing 'array' in the first code to 'file' in the second code I should have gotten the right answer because file, like array, is what was being searched for the string.
Does anyone see what I did wrong?
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 367
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 47
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Whiz
 
0
  #4
32 Days Ago
I passed this posting numerous times and I always wondered how do you define what your string is...i.e. can your string span more than one line in your text file, can your string overlap, can it overlap many times...with these questions bouncing around in my head the only way I can find to interrogate a text file is to start with the first character and check the preceding characters to see if you have a match, if you do increment count if not move to the next char and check the preceding characters....

Note with this method you have to disregard newline characters
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #5
32 Days Ago
If an alternate approach helps at all: strstr in this case, since it returns "a pointer to the located string (or null ptr if it isn't found)" you can (hypothetically) read your entire file into an array, then use strstr to find the first occurrence of whatever you're searching for. While you still find occurrences, keep calling strstr. The trick is to use pointer arithmetic to figure out where in the array to start searching on each iteration of your while loop. So basically: (pseudocode)

  1. while(ptr != null){
  2. - ptr = strstr(arrayposition, strToLookFor);
  3. - arrayposition = now do some pointer arithmetic to find out the place in the array where you just saw the last substring.
  4. }

(edit) after re-reading your code, it looks like you did already realize this. But maybe my approach will be a slightly easier approach.
Out.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 459
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 38
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #6
32 Days Ago
I got my function to work somewhat but it only prints the number of occurrences of the first word of the last line in the last line only. I just want to get the program to search all lines for the first word of the txt file.
Here is the revised code
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define MAX 1000
  5. #define SIZE 100
  6.  
  7. void search_string(char *file);
  8.  
  9. int main()
  10. {
  11. int counter = 0;
  12. const char filename[] = "in.txt";
  13. FILE *file = fopen(filename, "r");
  14. if ( file != NULL )
  15. {
  16. char line [ MAX ];
  17.  
  18. while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
  19. {
  20. ++counter;
  21. printf( "%d ", counter);
  22. fputs ( line, stdout ); /* write the line */
  23. }
  24. printf( "\n\n");
  25. search_string(line);
  26. fclose ( file );
  27. }
  28. else
  29. {
  30. perror ( filename );
  31. }
  32.  
  33. char wait;
  34. scanf( "%c", &wait );
  35. return(0);
  36. }
  37.  
  38.  
  39. void search_string(char *file)
  40. {
  41. char *ptr, *strptr, word[SIZE] = {" "};
  42. int i = 0, j = 0;
  43.  
  44. ptr = file;
  45.  
  46. while (*ptr != '\0')
  47. {
  48. if (isspace(*ptr))
  49. break;
  50. word[i] = file[i];
  51. ptr++;
  52. i++;
  53. }
  54. strptr = file;
  55.  
  56. while ((strptr = strstr(strptr, word)) != NULL)
  57. {
  58. strptr++;
  59. j++;
  60. }
  61.  
  62. printf("%s occured %d times in the string\n", word, j);
  63. }
If anyone has any ideas on how to implement my function to meet my needs I'd be very grateful. Thanks
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 50
Reputation: Iam3R is an unknown quantity at this point 
Solved Threads: 3
Iam3R Iam3R is offline Offline
Junior Poster in Training
 
0
  #7
32 Days Ago
I got my function to work somewhat but it only prints the number of occurrences of the first word of the last line in the last line only.
because you are calling the function search_string(line); after the statement

  1. while ( fgets ( line, sizeof line, file ) != NULL )
  2. {
  3. :
  4. :
  5. }
by the time it comes out of while line is having last line of the file.



I just want to get the program to search all lines for the first word of the txt file.
probably you want to search for the first word(Secret) in all line
you can modify so easily.

i have just edited your code, i dont completly understand your requirement , but this works fine for all the lines.
you can modify as per your Req:

Here is the revised code
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define MAX 1000
  5. #define SIZE 100
  6. void search_string(char *file);
  7.  
  8. int main()
  9. {
  10. int counter = 0;
  11. const char filename[] = "in.txt";
  12. FILE *file = fopen(filename, "r");
  13. if ( file != NULL )
  14. {
  15. char line [ MAX ];
  16.  
  17. while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
  18. {
  19. ++counter;
  20. printf( "%d ", counter);
  21. fputs ( line, stdout ); /* write the line */
  22. search_string(line);
  23. }
  24. printf( "\n\n");
  25. //search_string(line);
  26. fclose ( file );
  27. }
  28. else
  29. {
  30. perror ( filename );
  31. }
  32.  
  33. char wait;
  34. scanf( "%c", &wait );
  35. return(0);
  36. }
  37.  
  38.  
  39. void search_string(char *file)
  40. {
  41. char *ptr, *strptr, word[SIZE] = {" "};
  42. int i = 0, j = 0;
  43. int spc =0;
  44.  
  45. ptr = file;
  46.  
  47. while (*ptr != '\0')
  48. {
  49. if (isspace(*ptr) && spc == 0)
  50. {
  51. spc = 0;
  52. }
  53. if(spc != 0 && isspace(*ptr))
  54. break;
  55. if(isalnum(*ptr))
  56. {
  57. spc =1;
  58. word[i] = *ptr ; // file[i];
  59. /* here file[i] is blank , is a problem do you really want that*/
  60. i++;
  61. }
  62. ptr++;
  63. }
  64. word[i]='\0';
  65. strptr = file;
  66.  
  67. while ((strptr = strstr(strptr, word)) != NULL)
  68. {
  69. strptr++;
  70. j++;
  71. }
  72.  
  73. printf("%s occured %d times in the string\n", word, j);
  74. }

If anyone has any ideas on how to implement my function to meet my needs I'd be very grateful. Thanks
I Hope this does ...
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 459
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 38
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #8
32 Days Ago
Originally Posted by Iam3R View Post
probably you want to search for the first word(Secret) in all line
you can modify so easily.
Thanks for the reply. That however is not really what I'm looking for. Let me clarify. I want to search all the lines for the first word in the file. The code you supplied counts the first word of each line in that specific line. I have tried putting the functrion call in the while loop prior to your post and then just moved it back out when I posted. The above quote is actually what I'm looking for. I have tried to edit the code but have been unsuccessful. Would you mind giving me some more advice? Thanks.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 50
Reputation: Iam3R is an unknown quantity at this point 
Solved Threads: 3
Iam3R Iam3R is offline Offline
Junior Poster in Training
 
1
  #9
31 Days Ago
i hope this is what you want ..

i just edited , but some unneccessary statements/ variable are present that will not harm our prgram.

  1. #include<stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define MAX 1000
  5. #define SIZE 100
  6. void search_string(char *file, char *pat);
  7.  
  8. int main()
  9. {
  10. int counter = 0;
  11. int i=0;
  12. char ch;
  13. const char filename[] = "in.txt";
  14. char pat[50];
  15. FILE *file = fopen(filename, "r");
  16. if ( file != NULL )
  17. {
  18. char line [ MAX ];
  19. while(isspace(ch=fgetc(file))) ;
  20. pat[i++] = ch;
  21. while(!(isspace(ch=fgetc(file))))
  22. pat[i++] = ch;
  23. pat[i] = '\0';
  24. rewind(file);
  25. while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
  26. {
  27. ++counter;
  28. printf( "%d ", counter);
  29. fputs ( line, stdout ); /* write the line */
  30. search_string(line,pat);
  31. }
  32. printf( "\n\n");
  33. //search_string(line);
  34. fclose ( file );
  35. }
  36. else
  37. {
  38. perror ( filename );
  39. }
  40.  
  41. char wait;
  42. scanf( "%c", &wait );
  43. return(0);
  44. }
  45. void search_string(char *file, char *pat)
  46. {
  47. char *ptr, *strptr, word[SIZE] = {" "};
  48. int i = 0, j = 0;
  49. int spc =0;
  50. /*
  51.   while (*ptr != '\0')
  52.   {
  53.   if (isspace(*ptr) && spc == 0)
  54.   {
  55.   spc = 0;
  56.   }
  57.   if(spc != 0 && isspace(*ptr))
  58.   break;
  59.   if(isalnum(*ptr))
  60.   {
  61.   spc =1;
  62.   word[i] = *ptr ; // file[i];
  63.   / / here file[i] is blank , is a problem do you really want that
  64.   i++;
  65.   }
  66.   ptr++;
  67.   }
  68.   word[i]='\0';
  69.   */
  70.  
  71. strptr = file;
  72. while ((strptr = strstr(strptr, pat)) != NULL)
  73. {
  74. strptr++;
  75. j++;
  76. }
  77. printf("%s occured %d times in the string\n", pat, j);
  78. }


I have tried to edit the code but have been unsuccessful. Would you mind giving me some more advice? Thanks.
i hope this time i am Ryt.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 459
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 38
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #10
31 Days Ago
Thank you very much, that was very helpful.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC