Thread: Help with searching text file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    while(!feof(cfile)
    do not use feof to control loop - read FAQ
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why are you opening the text file, in append+ mode?

    Aren't you just reading the file, from first word to last word?

    This is what I think you were setting out to do with your program.


    Code:
    #include <stdio.h>
    #include <string.h>  //strlen()
    #include <ctype.h>   //isalpha()
    
    int main (void)
    {
    	//declare 
      FILE * cfile;	
      char ch;
      int len, j, i = 0;
      int count = 0;
    	
      char target[20]; //the word we are looking for
      char mystring[20] = { '\0' }; //the word from the file
    
    	/*************************************************************
    	 working with arrays and strings
    	*************************************************************/
    
       printf("\n\n\n\n"); //clean some space
    
    	/*************************************************************
    	 find file, write to it, output the string, end and close file
    	**************************************************************/
    	
    	//define and open text file
      cfile = fopen("blessing.txt", "rt");
    	
    	//error handling if file does not exist
      if(cfile == NULL) printf("Cannot open file");
    	
                // input target
      printf("Enter the target word I'm looking for: ");
      fgets(target, sizeof(target), stdin);
      printf("\nTarget: %s", target);
                //remove the newline fgets() adds, replace it with EOS char
      target[strlen(target) - 1] = '\0'; 
                
      
      for(i = 0; i < strlen(target); i++) //convert target to all lowercase
      {
         if(target[i] >= 'A' && target[i] <='Z')
    	target[i] = target[i] + 32; 
      }
      printf("\ntarget: %s", target);
    
    	/*************************************************************
    	         parse through file and search for string
    	**************************************************************/	
      i = 0; //prime the while "pump"
    
      while(1) {
        ch=fgetc(cfile);  
        if(ch == EOF)
          break;
        if(isalpha(ch)) {
          mystring[i] = ch;
          ++i;
        }
        else { //we should be at the end of the word
          //so set mystring's word, to all lowercase
          len = strlen(mystring);
          if(len == 0) 
            continue;
          for(j = 0; j <= len; j++) {
            if(mystring[j] >= 'A' && mystring[j] <= 'Z') 
              mystring[j] += ('a' - 'A');
          }
          //add the end of string char
          mystring[i+1] = '\0';
          //show nothing is going awry :)
          printf("\n mystring: %s", mystring);
          //see if the words are a match and count 'em up, if they are.
          if((strcmp(mystring, target)) == 0)
            count++;
          
          //remove the old word
          for(i = 0; i <= len; i++)
            mystring[i] = '\0';
          
          i = 0; //reset i for the next word
        }
      }
      //close file
      fclose(cfile);
    	
      //show user file has been written  
      //printf("\nSuccess. File has been written\n");
    	
      printf("\n\nYour search '%s' was found %d times \n", target, count);
    	
      printf("Press Enter to Continue...");
      j = getchar();
      return 0;
    }
    I used this phrase, in the file "blessing.txt":
    "For food, for raiment, for life and opportunity; for friendship and fellowship, we thank Thee, oh Lord."

    It works properly, in limited testing.
    Last edited by Adak; 12-01-2009 at 09:14 AM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    26
    At the bottom of the text file, I need to print what they searched for and how many times it showed up. This is why I was using a+. Is this not correct?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by zacharyrs View Post
    At the bottom of the text file, I need to print what they searched for and how many times it showed up. This is why I was using a+. Is this not correct?
    The file mode has nothing to do with printing out what the target was, or how many times it was found.

    You should use "r" for read mode, when you want to search through a file from front to back. Append mode is for writing onto the end of the file (appending the new data).

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    26
    Quote Originally Posted by Adak View Post
    The file mode has nothing to do with printing out what the target was, or how many times it was found.

    You should use "r" for read mode, when you want to search through a file from front to back.
    "r" will allow me to "read" and "write" to the file?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by zacharyrs View Post
    "r" will allow me to "read" and "write" to the file?
    No, only read.

    Do you want to write something to a file, as well?

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    26
    Quote Originally Posted by Adak View Post
    No, only read.

    Do you want to write something to a file, as well?
    Yeah I need to print - at the bottom of the text file - the mystring and target strings. that is why i was leaning towards the a+ cuz I thought it appended text at the end of the file?

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    26
    Weird, but great news: I changed 'a+' to 'r+' and it now works perfect.

    Thank you so much for your kind help. Now I'm going to go through and see what I can fix to make it better. If you have thoughts on how to make this better, so I can learn for future programming, that would be great

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    26
    Quote Originally Posted by Adak View Post
    Why are you opening the text file, in append+ mode?

    Aren't you just reading the file, from first word to last word?

    This is what I think you were setting out to do with your program.


    I used this phrase, in the file "blessing.txt":
    "For food, for raiment, for life and opportunity; for friendship and fellowship, we thank Thee, oh Lord."

    It works properly, in limited testing.
    Hmm. Looks like a lot of stuff I haven't learned yet in C At this point I'm just trying to get my program to work as it is... I really appreciate your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching a text file for double quotation marks "
    By willie in forum C Programming
    Replies: 4
    Last Post: 11-23-2008, 02:00 PM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:54 AM
  4. Read word from text file (It is an essay)
    By forfor in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 11:45 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM