944,113 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1450
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 3rd, 2009
0

find string in file

Expand Post »
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.
Attached Files
File Type: txt in.txt (526 Bytes, 32 views)
Last edited by Grn Xtrm; Nov 3rd, 2009 at 6:13 pm.
Similar Threads
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Nov 3rd, 2009
1
Re: find string in file
  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; Nov 3rd, 2009 at 6:54 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Nov 3rd, 2009
0
Re: find string in file
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?
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Nov 3rd, 2009
0
Re: find string in file
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
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is online now Online
2,198 posts
since Jan 2008
Nov 3rd, 2009
0
Re: find string in file
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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 3rd, 2009
0
Re: find string in file
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
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Nov 4th, 2009
0
Re: find string in file
Quote ...
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.



Quote ...
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. }

Quote ...
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 ...
Reputation Points: 34
Solved Threads: 4
Junior Poster
Iam3R is offline Offline
110 posts
since Oct 2009
Nov 4th, 2009
0
Re: find string in file
Click to Expand / Collapse  Quote originally posted by Iam3R ...
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.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Nov 5th, 2009
1
Re: find string in file
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. }


Quote ...
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.
Reputation Points: 34
Solved Threads: 4
Junior Poster
Iam3R is offline Offline
110 posts
since Oct 2009
Nov 5th, 2009
0
Re: find string in file
Thank you very much, that was very helpful.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Simple Problem give me solution
Next Thread in C Forum Timeline: Detect Multicore processors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC